]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c/c-typeck.c
Move MEMMODEL_* from coretypes.h to memmodel.h
[thirdparty/gcc.git] / gcc / c / c-typeck.c
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2016 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-low.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-ubsan.h"
49 #include "cilk.h"
50 #include "gomp-constants.h"
51 #include "spellcheck-tree.h"
52 #include "gcc-rich-location.h"
53
54 /* Possible cases of implicit bad conversions. Used to select
55 diagnostic messages in convert_for_assignment. */
56 enum impl_conv {
57 ic_argpass,
58 ic_assign,
59 ic_init,
60 ic_return
61 };
62
63 /* The level of nesting inside "__alignof__". */
64 int in_alignof;
65
66 /* The level of nesting inside "sizeof". */
67 int in_sizeof;
68
69 /* The level of nesting inside "typeof". */
70 int in_typeof;
71
72 /* The argument of last parsed sizeof expression, only to be tested
73 if expr.original_code == SIZEOF_EXPR. */
74 tree c_last_sizeof_arg;
75
76 /* Nonzero if we might need to print a "missing braces around
77 initializer" message within this initializer. */
78 static int found_missing_braces;
79
80 static int require_constant_value;
81 static int require_constant_elements;
82
83 static bool null_pointer_constant_p (const_tree);
84 static tree qualify_type (tree, tree);
85 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
86 bool *);
87 static int comp_target_types (location_t, tree, tree);
88 static int function_types_compatible_p (const_tree, const_tree, bool *,
89 bool *);
90 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
91 static tree lookup_field (tree, tree);
92 static int convert_arguments (location_t, vec<location_t>, tree,
93 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
94 tree);
95 static tree pointer_diff (location_t, tree, tree);
96 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
97 enum impl_conv, bool, tree, tree, int);
98 static tree valid_compound_expr_initializer (tree, tree);
99 static void push_string (const char *);
100 static void push_member_name (tree);
101 static int spelling_length (void);
102 static char *print_spelling (char *);
103 static void warning_init (location_t, int, const char *);
104 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
105 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
106 bool, struct obstack *);
107 static void output_pending_init_elements (int, struct obstack *);
108 static int set_designator (location_t, int, struct obstack *);
109 static void push_range_stack (tree, struct obstack *);
110 static void add_pending_init (location_t, tree, tree, tree, bool,
111 struct obstack *);
112 static void set_nonincremental_init (struct obstack *);
113 static void set_nonincremental_init_from_string (tree, struct obstack *);
114 static tree find_init_member (tree, struct obstack *);
115 static void readonly_warning (tree, enum lvalue_use);
116 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
117 static void record_maybe_used_decl (tree);
118 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
119 \f
120 /* Return true if EXP is a null pointer constant, false otherwise. */
121
122 static bool
123 null_pointer_constant_p (const_tree expr)
124 {
125 /* This should really operate on c_expr structures, but they aren't
126 yet available everywhere required. */
127 tree type = TREE_TYPE (expr);
128 return (TREE_CODE (expr) == INTEGER_CST
129 && !TREE_OVERFLOW (expr)
130 && integer_zerop (expr)
131 && (INTEGRAL_TYPE_P (type)
132 || (TREE_CODE (type) == POINTER_TYPE
133 && VOID_TYPE_P (TREE_TYPE (type))
134 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
135 }
136
137 /* EXPR may appear in an unevaluated part of an integer constant
138 expression, but not in an evaluated part. Wrap it in a
139 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
140 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
141
142 static tree
143 note_integer_operands (tree expr)
144 {
145 tree ret;
146 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
147 {
148 ret = copy_node (expr);
149 TREE_OVERFLOW (ret) = 1;
150 }
151 else
152 {
153 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
154 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
155 }
156 return ret;
157 }
158
159 /* Having checked whether EXPR may appear in an unevaluated part of an
160 integer constant expression and found that it may, remove any
161 C_MAYBE_CONST_EXPR noting this fact and return the resulting
162 expression. */
163
164 static inline tree
165 remove_c_maybe_const_expr (tree expr)
166 {
167 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
168 return C_MAYBE_CONST_EXPR_EXPR (expr);
169 else
170 return expr;
171 }
172
173 \f/* This is a cache to hold if two types are compatible or not. */
174
175 struct tagged_tu_seen_cache {
176 const struct tagged_tu_seen_cache * next;
177 const_tree t1;
178 const_tree t2;
179 /* The return value of tagged_types_tu_compatible_p if we had seen
180 these two types already. */
181 int val;
182 };
183
184 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
185 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
186
187 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
188 does not have an incomplete type. (That includes void types.)
189 LOC is the location of the use. */
190
191 tree
192 require_complete_type (location_t loc, tree value)
193 {
194 tree type = TREE_TYPE (value);
195
196 if (error_operand_p (value))
197 return error_mark_node;
198
199 /* First, detect a valid value with a complete type. */
200 if (COMPLETE_TYPE_P (type))
201 return value;
202
203 c_incomplete_type_error (loc, value, type);
204 return error_mark_node;
205 }
206
207 /* Print an error message for invalid use of an incomplete type.
208 VALUE is the expression that was used (or 0 if that isn't known)
209 and TYPE is the type that was invalid. LOC is the location for
210 the error. */
211
212 void
213 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
214 {
215 /* Avoid duplicate error message. */
216 if (TREE_CODE (type) == ERROR_MARK)
217 return;
218
219 if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
220 error_at (loc, "%qD has an incomplete type %qT", value, type);
221 else
222 {
223 retry:
224 /* We must print an error message. Be clever about what it says. */
225
226 switch (TREE_CODE (type))
227 {
228 case RECORD_TYPE:
229 case UNION_TYPE:
230 case ENUMERAL_TYPE:
231 break;
232
233 case VOID_TYPE:
234 error_at (loc, "invalid use of void expression");
235 return;
236
237 case ARRAY_TYPE:
238 if (TYPE_DOMAIN (type))
239 {
240 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
241 {
242 error_at (loc, "invalid use of flexible array member");
243 return;
244 }
245 type = TREE_TYPE (type);
246 goto retry;
247 }
248 error_at (loc, "invalid use of array with unspecified bounds");
249 return;
250
251 default:
252 gcc_unreachable ();
253 }
254
255 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
256 error_at (loc, "invalid use of undefined type %qT", type);
257 else
258 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
259 error_at (loc, "invalid use of incomplete typedef %qT", type);
260 }
261 }
262
263 /* Given a type, apply default promotions wrt unnamed function
264 arguments and return the new type. */
265
266 tree
267 c_type_promotes_to (tree type)
268 {
269 tree ret = NULL_TREE;
270
271 if (TYPE_MAIN_VARIANT (type) == float_type_node)
272 ret = double_type_node;
273 else if (c_promoting_integer_type_p (type))
274 {
275 /* Preserve unsignedness if not really getting any wider. */
276 if (TYPE_UNSIGNED (type)
277 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
278 ret = unsigned_type_node;
279 else
280 ret = integer_type_node;
281 }
282
283 if (ret != NULL_TREE)
284 return (TYPE_ATOMIC (type)
285 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
286 : ret);
287
288 return type;
289 }
290
291 /* Return true if between two named address spaces, whether there is a superset
292 named address space that encompasses both address spaces. If there is a
293 superset, return which address space is the superset. */
294
295 static bool
296 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
297 {
298 if (as1 == as2)
299 {
300 *common = as1;
301 return true;
302 }
303 else if (targetm.addr_space.subset_p (as1, as2))
304 {
305 *common = as2;
306 return true;
307 }
308 else if (targetm.addr_space.subset_p (as2, as1))
309 {
310 *common = as1;
311 return true;
312 }
313 else
314 return false;
315 }
316
317 /* Return a variant of TYPE which has all the type qualifiers of LIKE
318 as well as those of TYPE. */
319
320 static tree
321 qualify_type (tree type, tree like)
322 {
323 addr_space_t as_type = TYPE_ADDR_SPACE (type);
324 addr_space_t as_like = TYPE_ADDR_SPACE (like);
325 addr_space_t as_common;
326
327 /* If the two named address spaces are different, determine the common
328 superset address space. If there isn't one, raise an error. */
329 if (!addr_space_superset (as_type, as_like, &as_common))
330 {
331 as_common = as_type;
332 error ("%qT and %qT are in disjoint named address spaces",
333 type, like);
334 }
335
336 return c_build_qualified_type (type,
337 TYPE_QUALS_NO_ADDR_SPACE (type)
338 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
339 | ENCODE_QUAL_ADDR_SPACE (as_common));
340 }
341
342 /* Return true iff the given tree T is a variable length array. */
343
344 bool
345 c_vla_type_p (const_tree t)
346 {
347 if (TREE_CODE (t) == ARRAY_TYPE
348 && C_TYPE_VARIABLE_SIZE (t))
349 return true;
350 return false;
351 }
352 \f
353 /* Return the composite type of two compatible types.
354
355 We assume that comptypes has already been done and returned
356 nonzero; if that isn't so, this may crash. In particular, we
357 assume that qualifiers match. */
358
359 tree
360 composite_type (tree t1, tree t2)
361 {
362 enum tree_code code1;
363 enum tree_code code2;
364 tree attributes;
365
366 /* Save time if the two types are the same. */
367
368 if (t1 == t2) return t1;
369
370 /* If one type is nonsense, use the other. */
371 if (t1 == error_mark_node)
372 return t2;
373 if (t2 == error_mark_node)
374 return t1;
375
376 code1 = TREE_CODE (t1);
377 code2 = TREE_CODE (t2);
378
379 /* Merge the attributes. */
380 attributes = targetm.merge_type_attributes (t1, t2);
381
382 /* If one is an enumerated type and the other is the compatible
383 integer type, the composite type might be either of the two
384 (DR#013 question 3). For consistency, use the enumerated type as
385 the composite type. */
386
387 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
388 return t1;
389 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
390 return t2;
391
392 gcc_assert (code1 == code2);
393
394 switch (code1)
395 {
396 case POINTER_TYPE:
397 /* For two pointers, do this recursively on the target type. */
398 {
399 tree pointed_to_1 = TREE_TYPE (t1);
400 tree pointed_to_2 = TREE_TYPE (t2);
401 tree target = composite_type (pointed_to_1, pointed_to_2);
402 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
403 t1 = build_type_attribute_variant (t1, attributes);
404 return qualify_type (t1, t2);
405 }
406
407 case ARRAY_TYPE:
408 {
409 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
410 int quals;
411 tree unqual_elt;
412 tree d1 = TYPE_DOMAIN (t1);
413 tree d2 = TYPE_DOMAIN (t2);
414 bool d1_variable, d2_variable;
415 bool d1_zero, d2_zero;
416 bool t1_complete, t2_complete;
417
418 /* We should not have any type quals on arrays at all. */
419 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
420 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
421
422 t1_complete = COMPLETE_TYPE_P (t1);
423 t2_complete = COMPLETE_TYPE_P (t2);
424
425 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
426 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
427
428 d1_variable = (!d1_zero
429 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
430 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
431 d2_variable = (!d2_zero
432 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
433 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
434 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
435 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
436
437 /* Save space: see if the result is identical to one of the args. */
438 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
439 && (d2_variable || d2_zero || !d1_variable))
440 return build_type_attribute_variant (t1, attributes);
441 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
442 && (d1_variable || d1_zero || !d2_variable))
443 return build_type_attribute_variant (t2, attributes);
444
445 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
446 return build_type_attribute_variant (t1, attributes);
447 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
448 return build_type_attribute_variant (t2, attributes);
449
450 /* Merge the element types, and have a size if either arg has
451 one. We may have qualifiers on the element types. To set
452 up TYPE_MAIN_VARIANT correctly, we need to form the
453 composite of the unqualified types and add the qualifiers
454 back at the end. */
455 quals = TYPE_QUALS (strip_array_types (elt));
456 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
457 t1 = build_array_type (unqual_elt,
458 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
459 && (d2_variable
460 || d2_zero
461 || !d1_variable))
462 ? t1
463 : t2));
464 /* Ensure a composite type involving a zero-length array type
465 is a zero-length type not an incomplete type. */
466 if (d1_zero && d2_zero
467 && (t1_complete || t2_complete)
468 && !COMPLETE_TYPE_P (t1))
469 {
470 TYPE_SIZE (t1) = bitsize_zero_node;
471 TYPE_SIZE_UNIT (t1) = size_zero_node;
472 }
473 t1 = c_build_qualified_type (t1, quals);
474 return build_type_attribute_variant (t1, attributes);
475 }
476
477 case ENUMERAL_TYPE:
478 case RECORD_TYPE:
479 case UNION_TYPE:
480 if (attributes != NULL)
481 {
482 /* Try harder not to create a new aggregate type. */
483 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
484 return t1;
485 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
486 return t2;
487 }
488 return build_type_attribute_variant (t1, attributes);
489
490 case FUNCTION_TYPE:
491 /* Function types: prefer the one that specified arg types.
492 If both do, merge the arg types. Also merge the return types. */
493 {
494 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
495 tree p1 = TYPE_ARG_TYPES (t1);
496 tree p2 = TYPE_ARG_TYPES (t2);
497 int len;
498 tree newargs, n;
499 int i;
500
501 /* Save space: see if the result is identical to one of the args. */
502 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
503 return build_type_attribute_variant (t1, attributes);
504 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
505 return build_type_attribute_variant (t2, attributes);
506
507 /* Simple way if one arg fails to specify argument types. */
508 if (TYPE_ARG_TYPES (t1) == 0)
509 {
510 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
511 t1 = build_type_attribute_variant (t1, attributes);
512 return qualify_type (t1, t2);
513 }
514 if (TYPE_ARG_TYPES (t2) == 0)
515 {
516 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
517 t1 = build_type_attribute_variant (t1, attributes);
518 return qualify_type (t1, t2);
519 }
520
521 /* If both args specify argument types, we must merge the two
522 lists, argument by argument. */
523
524 for (len = 0, newargs = p1;
525 newargs && newargs != void_list_node;
526 len++, newargs = TREE_CHAIN (newargs))
527 ;
528
529 for (i = 0; i < len; i++)
530 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
531
532 n = newargs;
533
534 for (; p1 && p1 != void_list_node;
535 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
536 {
537 /* A null type means arg type is not specified.
538 Take whatever the other function type has. */
539 if (TREE_VALUE (p1) == 0)
540 {
541 TREE_VALUE (n) = TREE_VALUE (p2);
542 goto parm_done;
543 }
544 if (TREE_VALUE (p2) == 0)
545 {
546 TREE_VALUE (n) = TREE_VALUE (p1);
547 goto parm_done;
548 }
549
550 /* Given wait (union {union wait *u; int *i} *)
551 and wait (union wait *),
552 prefer union wait * as type of parm. */
553 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
554 && TREE_VALUE (p1) != TREE_VALUE (p2))
555 {
556 tree memb;
557 tree mv2 = TREE_VALUE (p2);
558 if (mv2 && mv2 != error_mark_node
559 && TREE_CODE (mv2) != ARRAY_TYPE)
560 mv2 = TYPE_MAIN_VARIANT (mv2);
561 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
562 memb; memb = DECL_CHAIN (memb))
563 {
564 tree mv3 = TREE_TYPE (memb);
565 if (mv3 && mv3 != error_mark_node
566 && TREE_CODE (mv3) != ARRAY_TYPE)
567 mv3 = TYPE_MAIN_VARIANT (mv3);
568 if (comptypes (mv3, mv2))
569 {
570 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
571 TREE_VALUE (p2));
572 pedwarn (input_location, OPT_Wpedantic,
573 "function types not truly compatible in ISO C");
574 goto parm_done;
575 }
576 }
577 }
578 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
579 && TREE_VALUE (p2) != TREE_VALUE (p1))
580 {
581 tree memb;
582 tree mv1 = TREE_VALUE (p1);
583 if (mv1 && mv1 != error_mark_node
584 && TREE_CODE (mv1) != ARRAY_TYPE)
585 mv1 = TYPE_MAIN_VARIANT (mv1);
586 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
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, mv1))
594 {
595 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
596 TREE_VALUE (p1));
597 pedwarn (input_location, OPT_Wpedantic,
598 "function types not truly compatible in ISO C");
599 goto parm_done;
600 }
601 }
602 }
603 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
604 parm_done: ;
605 }
606
607 t1 = build_function_type (valtype, newargs);
608 t1 = qualify_type (t1, t2);
609 }
610 /* FALLTHRU */
611
612 default:
613 return build_type_attribute_variant (t1, attributes);
614 }
615
616 }
617
618 /* Return the type of a conditional expression between pointers to
619 possibly differently qualified versions of compatible types.
620
621 We assume that comp_target_types has already been done and returned
622 nonzero; if that isn't so, this may crash. */
623
624 static tree
625 common_pointer_type (tree t1, tree t2)
626 {
627 tree attributes;
628 tree pointed_to_1, mv1;
629 tree pointed_to_2, mv2;
630 tree target;
631 unsigned target_quals;
632 addr_space_t as1, as2, as_common;
633 int quals1, quals2;
634
635 /* Save time if the two types are the same. */
636
637 if (t1 == t2) return t1;
638
639 /* If one type is nonsense, use the other. */
640 if (t1 == error_mark_node)
641 return t2;
642 if (t2 == error_mark_node)
643 return t1;
644
645 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
646 && TREE_CODE (t2) == POINTER_TYPE);
647
648 /* Merge the attributes. */
649 attributes = targetm.merge_type_attributes (t1, t2);
650
651 /* Find the composite type of the target types, and combine the
652 qualifiers of the two types' targets. Do not lose qualifiers on
653 array element types by taking the TYPE_MAIN_VARIANT. */
654 mv1 = pointed_to_1 = TREE_TYPE (t1);
655 mv2 = pointed_to_2 = TREE_TYPE (t2);
656 if (TREE_CODE (mv1) != ARRAY_TYPE)
657 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
658 if (TREE_CODE (mv2) != ARRAY_TYPE)
659 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
660 target = composite_type (mv1, mv2);
661
662 /* Strip array types to get correct qualifier for pointers to arrays */
663 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
664 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
665
666 /* For function types do not merge const qualifiers, but drop them
667 if used inconsistently. The middle-end uses these to mark const
668 and noreturn functions. */
669 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
670 target_quals = (quals1 & quals2);
671 else
672 target_quals = (quals1 | quals2);
673
674 /* If the two named address spaces are different, determine the common
675 superset address space. This is guaranteed to exist due to the
676 assumption that comp_target_type returned non-zero. */
677 as1 = TYPE_ADDR_SPACE (pointed_to_1);
678 as2 = TYPE_ADDR_SPACE (pointed_to_2);
679 if (!addr_space_superset (as1, as2, &as_common))
680 gcc_unreachable ();
681
682 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
683
684 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
685 return build_type_attribute_variant (t1, attributes);
686 }
687
688 /* Return the common type for two arithmetic types under the usual
689 arithmetic conversions. The default conversions have already been
690 applied, and enumerated types converted to their compatible integer
691 types. The resulting type is unqualified and has no attributes.
692
693 This is the type for the result of most arithmetic operations
694 if the operands have the given two types. */
695
696 static tree
697 c_common_type (tree t1, tree t2)
698 {
699 enum tree_code code1;
700 enum tree_code code2;
701
702 /* If one type is nonsense, use the other. */
703 if (t1 == error_mark_node)
704 return t2;
705 if (t2 == error_mark_node)
706 return t1;
707
708 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
709 t1 = TYPE_MAIN_VARIANT (t1);
710
711 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
712 t2 = TYPE_MAIN_VARIANT (t2);
713
714 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
715 t1 = build_type_attribute_variant (t1, NULL_TREE);
716
717 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
718 t2 = build_type_attribute_variant (t2, NULL_TREE);
719
720 /* Save time if the two types are the same. */
721
722 if (t1 == t2) return t1;
723
724 code1 = TREE_CODE (t1);
725 code2 = TREE_CODE (t2);
726
727 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
728 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
729 || code1 == INTEGER_TYPE);
730 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
731 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
732 || code2 == INTEGER_TYPE);
733
734 /* When one operand is a decimal float type, the other operand cannot be
735 a generic float type or a complex type. We also disallow vector types
736 here. */
737 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
738 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
739 {
740 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
741 {
742 error ("can%'t mix operands of decimal float and vector types");
743 return error_mark_node;
744 }
745 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
746 {
747 error ("can%'t mix operands of decimal float and complex types");
748 return error_mark_node;
749 }
750 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
751 {
752 error ("can%'t mix operands of decimal float and other float types");
753 return error_mark_node;
754 }
755 }
756
757 /* If one type is a vector type, return that type. (How the usual
758 arithmetic conversions apply to the vector types extension is not
759 precisely specified.) */
760 if (code1 == VECTOR_TYPE)
761 return t1;
762
763 if (code2 == VECTOR_TYPE)
764 return t2;
765
766 /* If one type is complex, form the common type of the non-complex
767 components, then make that complex. Use T1 or T2 if it is the
768 required type. */
769 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
770 {
771 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
772 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
773 tree subtype = c_common_type (subtype1, subtype2);
774
775 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
776 return t1;
777 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
778 return t2;
779 else
780 return build_complex_type (subtype);
781 }
782
783 /* If only one is real, use it as the result. */
784
785 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
786 return t1;
787
788 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
789 return t2;
790
791 /* If both are real and either are decimal floating point types, use
792 the decimal floating point type with the greater precision. */
793
794 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
795 {
796 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
797 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
798 return dfloat128_type_node;
799 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
800 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
801 return dfloat64_type_node;
802 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
803 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
804 return dfloat32_type_node;
805 }
806
807 /* Deal with fixed-point types. */
808 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
809 {
810 unsigned int unsignedp = 0, satp = 0;
811 machine_mode m1, m2;
812 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
813
814 m1 = TYPE_MODE (t1);
815 m2 = TYPE_MODE (t2);
816
817 /* If one input type is saturating, the result type is saturating. */
818 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
819 satp = 1;
820
821 /* If both fixed-point types are unsigned, the result type is unsigned.
822 When mixing fixed-point and integer types, follow the sign of the
823 fixed-point type.
824 Otherwise, the result type is signed. */
825 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
826 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
827 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
828 && TYPE_UNSIGNED (t1))
829 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
830 && TYPE_UNSIGNED (t2)))
831 unsignedp = 1;
832
833 /* The result type is signed. */
834 if (unsignedp == 0)
835 {
836 /* If the input type is unsigned, we need to convert to the
837 signed type. */
838 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
839 {
840 enum mode_class mclass = (enum mode_class) 0;
841 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
842 mclass = MODE_FRACT;
843 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
844 mclass = MODE_ACCUM;
845 else
846 gcc_unreachable ();
847 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
848 }
849 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
850 {
851 enum mode_class mclass = (enum mode_class) 0;
852 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
853 mclass = MODE_FRACT;
854 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
855 mclass = MODE_ACCUM;
856 else
857 gcc_unreachable ();
858 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
859 }
860 }
861
862 if (code1 == FIXED_POINT_TYPE)
863 {
864 fbit1 = GET_MODE_FBIT (m1);
865 ibit1 = GET_MODE_IBIT (m1);
866 }
867 else
868 {
869 fbit1 = 0;
870 /* Signed integers need to subtract one sign bit. */
871 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
872 }
873
874 if (code2 == FIXED_POINT_TYPE)
875 {
876 fbit2 = GET_MODE_FBIT (m2);
877 ibit2 = GET_MODE_IBIT (m2);
878 }
879 else
880 {
881 fbit2 = 0;
882 /* Signed integers need to subtract one sign bit. */
883 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
884 }
885
886 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
887 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
888 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
889 satp);
890 }
891
892 /* Both real or both integers; use the one with greater precision. */
893
894 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
895 return t1;
896 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
897 return t2;
898
899 /* Same precision. Prefer long longs to longs to ints when the
900 same precision, following the C99 rules on integer type rank
901 (which are equivalent to the C90 rules for C90 types). */
902
903 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
904 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
905 return long_long_unsigned_type_node;
906
907 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
908 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
909 {
910 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
911 return long_long_unsigned_type_node;
912 else
913 return long_long_integer_type_node;
914 }
915
916 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
917 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
918 return long_unsigned_type_node;
919
920 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
921 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
922 {
923 /* But preserve unsignedness from the other type,
924 since long cannot hold all the values of an unsigned int. */
925 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
926 return long_unsigned_type_node;
927 else
928 return long_integer_type_node;
929 }
930
931 /* For floating types of the same TYPE_PRECISION (which we here
932 assume means either the same set of values, or sets of values
933 neither a subset of the other, with behavior being undefined in
934 the latter case), follow the rules from TS 18661-3: prefer
935 interchange types _FloatN, then standard types long double,
936 double, float, then extended types _FloatNx. For extended types,
937 check them starting with _Float128x as that seems most consistent
938 in spirit with preferring long double to double; for interchange
939 types, also check in that order for consistency although it's not
940 possible for more than one of them to have the same
941 precision. */
942 tree mv1 = TYPE_MAIN_VARIANT (t1);
943 tree mv2 = TYPE_MAIN_VARIANT (t2);
944
945 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
946 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
947 return FLOATN_TYPE_NODE (i);
948
949 /* Likewise, prefer long double to double even if same size. */
950 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
951 return long_double_type_node;
952
953 /* Likewise, prefer double to float even if same size.
954 We got a couple of embedded targets with 32 bit doubles, and the
955 pdp11 might have 64 bit floats. */
956 if (mv1 == double_type_node || mv2 == double_type_node)
957 return double_type_node;
958
959 if (mv1 == float_type_node || mv2 == float_type_node)
960 return float_type_node;
961
962 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
963 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
964 return FLOATNX_TYPE_NODE (i);
965
966 /* Otherwise prefer the unsigned one. */
967
968 if (TYPE_UNSIGNED (t1))
969 return t1;
970 else
971 return t2;
972 }
973 \f
974 /* Wrapper around c_common_type that is used by c-common.c and other
975 front end optimizations that remove promotions. ENUMERAL_TYPEs
976 are allowed here and are converted to their compatible integer types.
977 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
978 preferably a non-Boolean type as the common type. */
979 tree
980 common_type (tree t1, tree t2)
981 {
982 if (TREE_CODE (t1) == ENUMERAL_TYPE)
983 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
984 if (TREE_CODE (t2) == ENUMERAL_TYPE)
985 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
986
987 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
988 if (TREE_CODE (t1) == BOOLEAN_TYPE
989 && TREE_CODE (t2) == BOOLEAN_TYPE)
990 return boolean_type_node;
991
992 /* If either type is BOOLEAN_TYPE, then return the other. */
993 if (TREE_CODE (t1) == BOOLEAN_TYPE)
994 return t2;
995 if (TREE_CODE (t2) == BOOLEAN_TYPE)
996 return t1;
997
998 return c_common_type (t1, t2);
999 }
1000
1001 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1002 or various other operations. Return 2 if they are compatible
1003 but a warning may be needed if you use them together. */
1004
1005 int
1006 comptypes (tree type1, tree type2)
1007 {
1008 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1009 int val;
1010
1011 val = comptypes_internal (type1, type2, NULL, NULL);
1012 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1013
1014 return val;
1015 }
1016
1017 /* Like comptypes, but if it returns non-zero because enum and int are
1018 compatible, it sets *ENUM_AND_INT_P to true. */
1019
1020 static int
1021 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1022 {
1023 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1024 int val;
1025
1026 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1027 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1028
1029 return val;
1030 }
1031
1032 /* Like comptypes, but if it returns nonzero for different types, it
1033 sets *DIFFERENT_TYPES_P to true. */
1034
1035 int
1036 comptypes_check_different_types (tree type1, tree type2,
1037 bool *different_types_p)
1038 {
1039 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1040 int val;
1041
1042 val = comptypes_internal (type1, type2, NULL, different_types_p);
1043 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1044
1045 return val;
1046 }
1047 \f
1048 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1049 or various other operations. Return 2 if they are compatible
1050 but a warning may be needed if you use them together. If
1051 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1052 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1053 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1054 NULL, and the types are compatible but different enough not to be
1055 permitted in C11 typedef redeclarations, then this sets
1056 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1057 false, but may or may not be set if the types are incompatible.
1058 This differs from comptypes, in that we don't free the seen
1059 types. */
1060
1061 static int
1062 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1063 bool *different_types_p)
1064 {
1065 const_tree t1 = type1;
1066 const_tree t2 = type2;
1067 int attrval, val;
1068
1069 /* Suppress errors caused by previously reported errors. */
1070
1071 if (t1 == t2 || !t1 || !t2
1072 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1073 return 1;
1074
1075 /* Enumerated types are compatible with integer types, but this is
1076 not transitive: two enumerated types in the same translation unit
1077 are compatible with each other only if they are the same type. */
1078
1079 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1080 {
1081 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1082 if (TREE_CODE (t2) != VOID_TYPE)
1083 {
1084 if (enum_and_int_p != NULL)
1085 *enum_and_int_p = true;
1086 if (different_types_p != NULL)
1087 *different_types_p = true;
1088 }
1089 }
1090 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1091 {
1092 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1093 if (TREE_CODE (t1) != VOID_TYPE)
1094 {
1095 if (enum_and_int_p != NULL)
1096 *enum_and_int_p = true;
1097 if (different_types_p != NULL)
1098 *different_types_p = true;
1099 }
1100 }
1101
1102 if (t1 == t2)
1103 return 1;
1104
1105 /* Different classes of types can't be compatible. */
1106
1107 if (TREE_CODE (t1) != TREE_CODE (t2))
1108 return 0;
1109
1110 /* Qualifiers must match. C99 6.7.3p9 */
1111
1112 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1113 return 0;
1114
1115 /* Allow for two different type nodes which have essentially the same
1116 definition. Note that we already checked for equality of the type
1117 qualifiers (just above). */
1118
1119 if (TREE_CODE (t1) != ARRAY_TYPE
1120 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1121 return 1;
1122
1123 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1124 if (!(attrval = comp_type_attributes (t1, t2)))
1125 return 0;
1126
1127 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1128 val = 0;
1129
1130 switch (TREE_CODE (t1))
1131 {
1132 case INTEGER_TYPE:
1133 case FIXED_POINT_TYPE:
1134 case REAL_TYPE:
1135 /* With these nodes, we can't determine type equivalence by
1136 looking at what is stored in the nodes themselves, because
1137 two nodes might have different TYPE_MAIN_VARIANTs but still
1138 represent the same type. For example, wchar_t and int could
1139 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1140 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1141 and are distinct types. On the other hand, int and the
1142 following typedef
1143
1144 typedef int INT __attribute((may_alias));
1145
1146 have identical properties, different TYPE_MAIN_VARIANTs, but
1147 represent the same type. The canonical type system keeps
1148 track of equivalence in this case, so we fall back on it. */
1149 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1150
1151 case POINTER_TYPE:
1152 /* Do not remove mode information. */
1153 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1154 break;
1155 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1156 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1157 enum_and_int_p, different_types_p));
1158 break;
1159
1160 case FUNCTION_TYPE:
1161 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1162 different_types_p);
1163 break;
1164
1165 case ARRAY_TYPE:
1166 {
1167 tree d1 = TYPE_DOMAIN (t1);
1168 tree d2 = TYPE_DOMAIN (t2);
1169 bool d1_variable, d2_variable;
1170 bool d1_zero, d2_zero;
1171 val = 1;
1172
1173 /* Target types must match incl. qualifiers. */
1174 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1175 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1176 enum_and_int_p,
1177 different_types_p)))
1178 return 0;
1179
1180 if (different_types_p != NULL
1181 && (d1 == 0) != (d2 == 0))
1182 *different_types_p = true;
1183 /* Sizes must match unless one is missing or variable. */
1184 if (d1 == 0 || d2 == 0 || d1 == d2)
1185 break;
1186
1187 d1_zero = !TYPE_MAX_VALUE (d1);
1188 d2_zero = !TYPE_MAX_VALUE (d2);
1189
1190 d1_variable = (!d1_zero
1191 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1192 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1193 d2_variable = (!d2_zero
1194 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1195 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1196 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1197 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1198
1199 if (different_types_p != NULL
1200 && d1_variable != d2_variable)
1201 *different_types_p = true;
1202 if (d1_variable || d2_variable)
1203 break;
1204 if (d1_zero && d2_zero)
1205 break;
1206 if (d1_zero || d2_zero
1207 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1208 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1209 val = 0;
1210
1211 break;
1212 }
1213
1214 case ENUMERAL_TYPE:
1215 case RECORD_TYPE:
1216 case UNION_TYPE:
1217 if (val != 1 && !same_translation_unit_p (t1, t2))
1218 {
1219 tree a1 = TYPE_ATTRIBUTES (t1);
1220 tree a2 = TYPE_ATTRIBUTES (t2);
1221
1222 if (! attribute_list_contained (a1, a2)
1223 && ! attribute_list_contained (a2, a1))
1224 break;
1225
1226 if (attrval != 2)
1227 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1228 different_types_p);
1229 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1230 different_types_p);
1231 }
1232 break;
1233
1234 case VECTOR_TYPE:
1235 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1236 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1237 enum_and_int_p, different_types_p));
1238 break;
1239
1240 default:
1241 break;
1242 }
1243 return attrval == 2 && val == 1 ? 2 : val;
1244 }
1245
1246 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1247 their qualifiers, except for named address spaces. If the pointers point to
1248 different named addresses, then we must determine if one address space is a
1249 subset of the other. */
1250
1251 static int
1252 comp_target_types (location_t location, tree ttl, tree ttr)
1253 {
1254 int val;
1255 int val_ped;
1256 tree mvl = TREE_TYPE (ttl);
1257 tree mvr = TREE_TYPE (ttr);
1258 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1259 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1260 addr_space_t as_common;
1261 bool enum_and_int_p;
1262
1263 /* Fail if pointers point to incompatible address spaces. */
1264 if (!addr_space_superset (asl, asr, &as_common))
1265 return 0;
1266
1267 /* For pedantic record result of comptypes on arrays before losing
1268 qualifiers on the element type below. */
1269 val_ped = 1;
1270
1271 if (TREE_CODE (mvl) == ARRAY_TYPE
1272 && TREE_CODE (mvr) == ARRAY_TYPE)
1273 val_ped = comptypes (mvl, mvr);
1274
1275 /* Qualifiers on element types of array types that are
1276 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1277
1278 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1279 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1280 : TYPE_MAIN_VARIANT (mvl));
1281
1282 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1283 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1284 : TYPE_MAIN_VARIANT (mvr));
1285
1286 enum_and_int_p = false;
1287 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1288
1289 if (val == 1 && val_ped != 1)
1290 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1291 "are incompatible in ISO C");
1292
1293 if (val == 2)
1294 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1295
1296 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1297 warning_at (location, OPT_Wc___compat,
1298 "pointer target types incompatible in C++");
1299
1300 return val;
1301 }
1302 \f
1303 /* Subroutines of `comptypes'. */
1304
1305 /* Determine whether two trees derive from the same translation unit.
1306 If the CONTEXT chain ends in a null, that tree's context is still
1307 being parsed, so if two trees have context chains ending in null,
1308 they're in the same translation unit. */
1309 int
1310 same_translation_unit_p (const_tree t1, const_tree t2)
1311 {
1312 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1313 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1314 {
1315 case tcc_declaration:
1316 t1 = DECL_CONTEXT (t1); break;
1317 case tcc_type:
1318 t1 = TYPE_CONTEXT (t1); break;
1319 case tcc_exceptional:
1320 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1321 default: gcc_unreachable ();
1322 }
1323
1324 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1325 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1326 {
1327 case tcc_declaration:
1328 t2 = DECL_CONTEXT (t2); break;
1329 case tcc_type:
1330 t2 = TYPE_CONTEXT (t2); break;
1331 case tcc_exceptional:
1332 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1333 default: gcc_unreachable ();
1334 }
1335
1336 return t1 == t2;
1337 }
1338
1339 /* Allocate the seen two types, assuming that they are compatible. */
1340
1341 static struct tagged_tu_seen_cache *
1342 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1343 {
1344 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1345 tu->next = tagged_tu_seen_base;
1346 tu->t1 = t1;
1347 tu->t2 = t2;
1348
1349 tagged_tu_seen_base = tu;
1350
1351 /* The C standard says that two structures in different translation
1352 units are compatible with each other only if the types of their
1353 fields are compatible (among other things). We assume that they
1354 are compatible until proven otherwise when building the cache.
1355 An example where this can occur is:
1356 struct a
1357 {
1358 struct a *next;
1359 };
1360 If we are comparing this against a similar struct in another TU,
1361 and did not assume they were compatible, we end up with an infinite
1362 loop. */
1363 tu->val = 1;
1364 return tu;
1365 }
1366
1367 /* Free the seen types until we get to TU_TIL. */
1368
1369 static void
1370 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1371 {
1372 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1373 while (tu != tu_til)
1374 {
1375 const struct tagged_tu_seen_cache *const tu1
1376 = (const struct tagged_tu_seen_cache *) tu;
1377 tu = tu1->next;
1378 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1379 }
1380 tagged_tu_seen_base = tu_til;
1381 }
1382
1383 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1384 compatible. If the two types are not the same (which has been
1385 checked earlier), this can only happen when multiple translation
1386 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1387 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1388 comptypes_internal. */
1389
1390 static int
1391 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1392 bool *enum_and_int_p, bool *different_types_p)
1393 {
1394 tree s1, s2;
1395 bool needs_warning = false;
1396
1397 /* We have to verify that the tags of the types are the same. This
1398 is harder than it looks because this may be a typedef, so we have
1399 to go look at the original type. It may even be a typedef of a
1400 typedef...
1401 In the case of compiler-created builtin structs the TYPE_DECL
1402 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1403 while (TYPE_NAME (t1)
1404 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1405 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1406 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1407
1408 while (TYPE_NAME (t2)
1409 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1410 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1411 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1412
1413 /* C90 didn't have the requirement that the two tags be the same. */
1414 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1415 return 0;
1416
1417 /* C90 didn't say what happened if one or both of the types were
1418 incomplete; we choose to follow C99 rules here, which is that they
1419 are compatible. */
1420 if (TYPE_SIZE (t1) == NULL
1421 || TYPE_SIZE (t2) == NULL)
1422 return 1;
1423
1424 {
1425 const struct tagged_tu_seen_cache * tts_i;
1426 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1427 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1428 return tts_i->val;
1429 }
1430
1431 switch (TREE_CODE (t1))
1432 {
1433 case ENUMERAL_TYPE:
1434 {
1435 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1436 /* Speed up the case where the type values are in the same order. */
1437 tree tv1 = TYPE_VALUES (t1);
1438 tree tv2 = TYPE_VALUES (t2);
1439
1440 if (tv1 == tv2)
1441 {
1442 return 1;
1443 }
1444
1445 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1446 {
1447 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1448 break;
1449 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1450 {
1451 tu->val = 0;
1452 return 0;
1453 }
1454 }
1455
1456 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1457 {
1458 return 1;
1459 }
1460 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1461 {
1462 tu->val = 0;
1463 return 0;
1464 }
1465
1466 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1467 {
1468 tu->val = 0;
1469 return 0;
1470 }
1471
1472 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1473 {
1474 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1475 if (s2 == NULL
1476 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1477 {
1478 tu->val = 0;
1479 return 0;
1480 }
1481 }
1482 return 1;
1483 }
1484
1485 case UNION_TYPE:
1486 {
1487 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1488 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1489 {
1490 tu->val = 0;
1491 return 0;
1492 }
1493
1494 /* Speed up the common case where the fields are in the same order. */
1495 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1496 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1497 {
1498 int result;
1499
1500 if (DECL_NAME (s1) != DECL_NAME (s2))
1501 break;
1502 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1503 enum_and_int_p, different_types_p);
1504
1505 if (result != 1 && !DECL_NAME (s1))
1506 break;
1507 if (result == 0)
1508 {
1509 tu->val = 0;
1510 return 0;
1511 }
1512 if (result == 2)
1513 needs_warning = true;
1514
1515 if (TREE_CODE (s1) == FIELD_DECL
1516 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1517 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1518 {
1519 tu->val = 0;
1520 return 0;
1521 }
1522 }
1523 if (!s1 && !s2)
1524 {
1525 tu->val = needs_warning ? 2 : 1;
1526 return tu->val;
1527 }
1528
1529 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1530 {
1531 bool ok = false;
1532
1533 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1534 if (DECL_NAME (s1) == DECL_NAME (s2))
1535 {
1536 int result;
1537
1538 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1539 enum_and_int_p,
1540 different_types_p);
1541
1542 if (result != 1 && !DECL_NAME (s1))
1543 continue;
1544 if (result == 0)
1545 {
1546 tu->val = 0;
1547 return 0;
1548 }
1549 if (result == 2)
1550 needs_warning = true;
1551
1552 if (TREE_CODE (s1) == FIELD_DECL
1553 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1554 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1555 break;
1556
1557 ok = true;
1558 break;
1559 }
1560 if (!ok)
1561 {
1562 tu->val = 0;
1563 return 0;
1564 }
1565 }
1566 tu->val = needs_warning ? 2 : 10;
1567 return tu->val;
1568 }
1569
1570 case RECORD_TYPE:
1571 {
1572 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1573
1574 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1575 s1 && s2;
1576 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1577 {
1578 int result;
1579 if (TREE_CODE (s1) != TREE_CODE (s2)
1580 || DECL_NAME (s1) != DECL_NAME (s2))
1581 break;
1582 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1583 enum_and_int_p, different_types_p);
1584 if (result == 0)
1585 break;
1586 if (result == 2)
1587 needs_warning = true;
1588
1589 if (TREE_CODE (s1) == FIELD_DECL
1590 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1591 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1592 break;
1593 }
1594 if (s1 && s2)
1595 tu->val = 0;
1596 else
1597 tu->val = needs_warning ? 2 : 1;
1598 return tu->val;
1599 }
1600
1601 default:
1602 gcc_unreachable ();
1603 }
1604 }
1605
1606 /* Return 1 if two function types F1 and F2 are compatible.
1607 If either type specifies no argument types,
1608 the other must specify a fixed number of self-promoting arg types.
1609 Otherwise, if one type specifies only the number of arguments,
1610 the other must specify that number of self-promoting arg types.
1611 Otherwise, the argument types must match.
1612 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1613
1614 static int
1615 function_types_compatible_p (const_tree f1, const_tree f2,
1616 bool *enum_and_int_p, bool *different_types_p)
1617 {
1618 tree args1, args2;
1619 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1620 int val = 1;
1621 int val1;
1622 tree ret1, ret2;
1623
1624 ret1 = TREE_TYPE (f1);
1625 ret2 = TREE_TYPE (f2);
1626
1627 /* 'volatile' qualifiers on a function's return type used to mean
1628 the function is noreturn. */
1629 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1630 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1631 if (TYPE_VOLATILE (ret1))
1632 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1633 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1634 if (TYPE_VOLATILE (ret2))
1635 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1636 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1637 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1638 if (val == 0)
1639 return 0;
1640
1641 args1 = TYPE_ARG_TYPES (f1);
1642 args2 = TYPE_ARG_TYPES (f2);
1643
1644 if (different_types_p != NULL
1645 && (args1 == 0) != (args2 == 0))
1646 *different_types_p = true;
1647
1648 /* An unspecified parmlist matches any specified parmlist
1649 whose argument types don't need default promotions. */
1650
1651 if (args1 == 0)
1652 {
1653 if (!self_promoting_args_p (args2))
1654 return 0;
1655 /* If one of these types comes from a non-prototype fn definition,
1656 compare that with the other type's arglist.
1657 If they don't match, ask for a warning (but no error). */
1658 if (TYPE_ACTUAL_ARG_TYPES (f1)
1659 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1660 enum_and_int_p, different_types_p))
1661 val = 2;
1662 return val;
1663 }
1664 if (args2 == 0)
1665 {
1666 if (!self_promoting_args_p (args1))
1667 return 0;
1668 if (TYPE_ACTUAL_ARG_TYPES (f2)
1669 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1670 enum_and_int_p, different_types_p))
1671 val = 2;
1672 return val;
1673 }
1674
1675 /* Both types have argument lists: compare them and propagate results. */
1676 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1677 different_types_p);
1678 return val1 != 1 ? val1 : val;
1679 }
1680
1681 /* Check two lists of types for compatibility, returning 0 for
1682 incompatible, 1 for compatible, or 2 for compatible with
1683 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1684 comptypes_internal. */
1685
1686 static int
1687 type_lists_compatible_p (const_tree args1, const_tree args2,
1688 bool *enum_and_int_p, bool *different_types_p)
1689 {
1690 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1691 int val = 1;
1692 int newval = 0;
1693
1694 while (1)
1695 {
1696 tree a1, mv1, a2, mv2;
1697 if (args1 == 0 && args2 == 0)
1698 return val;
1699 /* If one list is shorter than the other,
1700 they fail to match. */
1701 if (args1 == 0 || args2 == 0)
1702 return 0;
1703 mv1 = a1 = TREE_VALUE (args1);
1704 mv2 = a2 = TREE_VALUE (args2);
1705 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1706 mv1 = (TYPE_ATOMIC (mv1)
1707 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1708 TYPE_QUAL_ATOMIC)
1709 : TYPE_MAIN_VARIANT (mv1));
1710 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1711 mv2 = (TYPE_ATOMIC (mv2)
1712 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1713 TYPE_QUAL_ATOMIC)
1714 : TYPE_MAIN_VARIANT (mv2));
1715 /* A null pointer instead of a type
1716 means there is supposed to be an argument
1717 but nothing is specified about what type it has.
1718 So match anything that self-promotes. */
1719 if (different_types_p != NULL
1720 && (a1 == 0) != (a2 == 0))
1721 *different_types_p = true;
1722 if (a1 == 0)
1723 {
1724 if (c_type_promotes_to (a2) != a2)
1725 return 0;
1726 }
1727 else if (a2 == 0)
1728 {
1729 if (c_type_promotes_to (a1) != a1)
1730 return 0;
1731 }
1732 /* If one of the lists has an error marker, ignore this arg. */
1733 else if (TREE_CODE (a1) == ERROR_MARK
1734 || TREE_CODE (a2) == ERROR_MARK)
1735 ;
1736 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1737 different_types_p)))
1738 {
1739 if (different_types_p != NULL)
1740 *different_types_p = true;
1741 /* Allow wait (union {union wait *u; int *i} *)
1742 and wait (union wait *) to be compatible. */
1743 if (TREE_CODE (a1) == UNION_TYPE
1744 && (TYPE_NAME (a1) == 0
1745 || TYPE_TRANSPARENT_AGGR (a1))
1746 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1747 && tree_int_cst_equal (TYPE_SIZE (a1),
1748 TYPE_SIZE (a2)))
1749 {
1750 tree memb;
1751 for (memb = TYPE_FIELDS (a1);
1752 memb; memb = DECL_CHAIN (memb))
1753 {
1754 tree mv3 = TREE_TYPE (memb);
1755 if (mv3 && mv3 != error_mark_node
1756 && TREE_CODE (mv3) != ARRAY_TYPE)
1757 mv3 = (TYPE_ATOMIC (mv3)
1758 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1759 TYPE_QUAL_ATOMIC)
1760 : TYPE_MAIN_VARIANT (mv3));
1761 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1762 different_types_p))
1763 break;
1764 }
1765 if (memb == 0)
1766 return 0;
1767 }
1768 else if (TREE_CODE (a2) == UNION_TYPE
1769 && (TYPE_NAME (a2) == 0
1770 || TYPE_TRANSPARENT_AGGR (a2))
1771 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1772 && tree_int_cst_equal (TYPE_SIZE (a2),
1773 TYPE_SIZE (a1)))
1774 {
1775 tree memb;
1776 for (memb = TYPE_FIELDS (a2);
1777 memb; memb = DECL_CHAIN (memb))
1778 {
1779 tree mv3 = TREE_TYPE (memb);
1780 if (mv3 && mv3 != error_mark_node
1781 && TREE_CODE (mv3) != ARRAY_TYPE)
1782 mv3 = (TYPE_ATOMIC (mv3)
1783 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1784 TYPE_QUAL_ATOMIC)
1785 : TYPE_MAIN_VARIANT (mv3));
1786 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1787 different_types_p))
1788 break;
1789 }
1790 if (memb == 0)
1791 return 0;
1792 }
1793 else
1794 return 0;
1795 }
1796
1797 /* comptypes said ok, but record if it said to warn. */
1798 if (newval > val)
1799 val = newval;
1800
1801 args1 = TREE_CHAIN (args1);
1802 args2 = TREE_CHAIN (args2);
1803 }
1804 }
1805 \f
1806 /* Compute the size to increment a pointer by. When a function type or void
1807 type or incomplete type is passed, size_one_node is returned.
1808 This function does not emit any diagnostics; the caller is responsible
1809 for that. */
1810
1811 static tree
1812 c_size_in_bytes (const_tree type)
1813 {
1814 enum tree_code code = TREE_CODE (type);
1815
1816 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1817 || !COMPLETE_TYPE_P (type))
1818 return size_one_node;
1819
1820 /* Convert in case a char is more than one unit. */
1821 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1822 size_int (TYPE_PRECISION (char_type_node)
1823 / BITS_PER_UNIT));
1824 }
1825 \f
1826 /* Return either DECL or its known constant value (if it has one). */
1827
1828 tree
1829 decl_constant_value (tree decl)
1830 {
1831 if (/* Don't change a variable array bound or initial value to a constant
1832 in a place where a variable is invalid. Note that DECL_INITIAL
1833 isn't valid for a PARM_DECL. */
1834 current_function_decl != 0
1835 && TREE_CODE (decl) != PARM_DECL
1836 && !TREE_THIS_VOLATILE (decl)
1837 && TREE_READONLY (decl)
1838 && DECL_INITIAL (decl) != 0
1839 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1840 /* This is invalid if initial value is not constant.
1841 If it has either a function call, a memory reference,
1842 or a variable, then re-evaluating it could give different results. */
1843 && TREE_CONSTANT (DECL_INITIAL (decl))
1844 /* Check for cases where this is sub-optimal, even though valid. */
1845 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1846 return DECL_INITIAL (decl);
1847 return decl;
1848 }
1849
1850 /* Convert the array expression EXP to a pointer. */
1851 static tree
1852 array_to_pointer_conversion (location_t loc, tree exp)
1853 {
1854 tree orig_exp = exp;
1855 tree type = TREE_TYPE (exp);
1856 tree adr;
1857 tree restype = TREE_TYPE (type);
1858 tree ptrtype;
1859
1860 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1861
1862 STRIP_TYPE_NOPS (exp);
1863
1864 if (TREE_NO_WARNING (orig_exp))
1865 TREE_NO_WARNING (exp) = 1;
1866
1867 ptrtype = build_pointer_type (restype);
1868
1869 if (INDIRECT_REF_P (exp))
1870 return convert (ptrtype, TREE_OPERAND (exp, 0));
1871
1872 /* In C++ array compound literals are temporary objects unless they are
1873 const or appear in namespace scope, so they are destroyed too soon
1874 to use them for much of anything (c++/53220). */
1875 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1876 {
1877 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1878 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1879 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1880 "converting an array compound literal to a pointer "
1881 "is ill-formed in C++");
1882 }
1883
1884 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1885 return convert (ptrtype, adr);
1886 }
1887
1888 /* Convert the function expression EXP to a pointer. */
1889 static tree
1890 function_to_pointer_conversion (location_t loc, tree exp)
1891 {
1892 tree orig_exp = exp;
1893
1894 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1895
1896 STRIP_TYPE_NOPS (exp);
1897
1898 if (TREE_NO_WARNING (orig_exp))
1899 TREE_NO_WARNING (exp) = 1;
1900
1901 return build_unary_op (loc, ADDR_EXPR, exp, false);
1902 }
1903
1904 /* Mark EXP as read, not just set, for set but not used -Wunused
1905 warning purposes. */
1906
1907 void
1908 mark_exp_read (tree exp)
1909 {
1910 switch (TREE_CODE (exp))
1911 {
1912 case VAR_DECL:
1913 case PARM_DECL:
1914 DECL_READ_P (exp) = 1;
1915 break;
1916 case ARRAY_REF:
1917 case COMPONENT_REF:
1918 case MODIFY_EXPR:
1919 case REALPART_EXPR:
1920 case IMAGPART_EXPR:
1921 CASE_CONVERT:
1922 case ADDR_EXPR:
1923 case VIEW_CONVERT_EXPR:
1924 mark_exp_read (TREE_OPERAND (exp, 0));
1925 break;
1926 case COMPOUND_EXPR:
1927 case C_MAYBE_CONST_EXPR:
1928 mark_exp_read (TREE_OPERAND (exp, 1));
1929 break;
1930 default:
1931 break;
1932 }
1933 }
1934
1935 /* Perform the default conversion of arrays and functions to pointers.
1936 Return the result of converting EXP. For any other expression, just
1937 return EXP.
1938
1939 LOC is the location of the expression. */
1940
1941 struct c_expr
1942 default_function_array_conversion (location_t loc, struct c_expr exp)
1943 {
1944 tree orig_exp = exp.value;
1945 tree type = TREE_TYPE (exp.value);
1946 enum tree_code code = TREE_CODE (type);
1947
1948 switch (code)
1949 {
1950 case ARRAY_TYPE:
1951 {
1952 bool not_lvalue = false;
1953 bool lvalue_array_p;
1954
1955 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1956 || CONVERT_EXPR_P (exp.value))
1957 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1958 {
1959 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1960 not_lvalue = true;
1961 exp.value = TREE_OPERAND (exp.value, 0);
1962 }
1963
1964 if (TREE_NO_WARNING (orig_exp))
1965 TREE_NO_WARNING (exp.value) = 1;
1966
1967 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1968 if (!flag_isoc99 && !lvalue_array_p)
1969 {
1970 /* Before C99, non-lvalue arrays do not decay to pointers.
1971 Normally, using such an array would be invalid; but it can
1972 be used correctly inside sizeof or as a statement expression.
1973 Thus, do not give an error here; an error will result later. */
1974 return exp;
1975 }
1976
1977 exp.value = array_to_pointer_conversion (loc, exp.value);
1978 }
1979 break;
1980 case FUNCTION_TYPE:
1981 exp.value = function_to_pointer_conversion (loc, exp.value);
1982 break;
1983 default:
1984 break;
1985 }
1986
1987 return exp;
1988 }
1989
1990 struct c_expr
1991 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1992 {
1993 mark_exp_read (exp.value);
1994 return default_function_array_conversion (loc, exp);
1995 }
1996
1997 /* Return whether EXPR should be treated as an atomic lvalue for the
1998 purposes of load and store handling. */
1999
2000 static bool
2001 really_atomic_lvalue (tree expr)
2002 {
2003 if (error_operand_p (expr))
2004 return false;
2005 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2006 return false;
2007 if (!lvalue_p (expr))
2008 return false;
2009
2010 /* Ignore _Atomic on register variables, since their addresses can't
2011 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2012 sequences wouldn't work. Ignore _Atomic on structures containing
2013 bit-fields, since accessing elements of atomic structures or
2014 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2015 it's undefined at translation time or execution time, and the
2016 normal atomic sequences again wouldn't work. */
2017 while (handled_component_p (expr))
2018 {
2019 if (TREE_CODE (expr) == COMPONENT_REF
2020 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2021 return false;
2022 expr = TREE_OPERAND (expr, 0);
2023 }
2024 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2025 return false;
2026 return true;
2027 }
2028
2029 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2030 including converting functions and arrays to pointers if CONVERT_P.
2031 If READ_P, also mark the expression as having been read. */
2032
2033 struct c_expr
2034 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2035 bool convert_p, bool read_p)
2036 {
2037 if (read_p)
2038 mark_exp_read (exp.value);
2039 if (convert_p)
2040 exp = default_function_array_conversion (loc, exp);
2041 if (really_atomic_lvalue (exp.value))
2042 {
2043 vec<tree, va_gc> *params;
2044 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2045 tree expr_type = TREE_TYPE (exp.value);
2046 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2047 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2048
2049 gcc_assert (TYPE_ATOMIC (expr_type));
2050
2051 /* Expansion of a generic atomic load may require an addition
2052 element, so allocate enough to prevent a resize. */
2053 vec_alloc (params, 4);
2054
2055 /* Remove the qualifiers for the rest of the expressions and
2056 create the VAL temp variable to hold the RHS. */
2057 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2058 tmp = create_tmp_var_raw (nonatomic_type);
2059 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2060 TREE_ADDRESSABLE (tmp) = 1;
2061 TREE_NO_WARNING (tmp) = 1;
2062
2063 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2064 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2065 params->quick_push (expr_addr);
2066 params->quick_push (tmp_addr);
2067 params->quick_push (seq_cst);
2068 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2069
2070 /* EXPR is always read. */
2071 mark_exp_read (exp.value);
2072
2073 /* Return tmp which contains the value loaded. */
2074 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2075 NULL_TREE, NULL_TREE);
2076 }
2077 return exp;
2078 }
2079
2080 /* EXP is an expression of integer type. Apply the integer promotions
2081 to it and return the promoted value. */
2082
2083 tree
2084 perform_integral_promotions (tree exp)
2085 {
2086 tree type = TREE_TYPE (exp);
2087 enum tree_code code = TREE_CODE (type);
2088
2089 gcc_assert (INTEGRAL_TYPE_P (type));
2090
2091 /* Normally convert enums to int,
2092 but convert wide enums to something wider. */
2093 if (code == ENUMERAL_TYPE)
2094 {
2095 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2096 TYPE_PRECISION (integer_type_node)),
2097 ((TYPE_PRECISION (type)
2098 >= TYPE_PRECISION (integer_type_node))
2099 && TYPE_UNSIGNED (type)));
2100
2101 return convert (type, exp);
2102 }
2103
2104 /* ??? This should no longer be needed now bit-fields have their
2105 proper types. */
2106 if (TREE_CODE (exp) == COMPONENT_REF
2107 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2108 /* If it's thinner than an int, promote it like a
2109 c_promoting_integer_type_p, otherwise leave it alone. */
2110 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2111 TYPE_PRECISION (integer_type_node)))
2112 return convert (integer_type_node, exp);
2113
2114 if (c_promoting_integer_type_p (type))
2115 {
2116 /* Preserve unsignedness if not really getting any wider. */
2117 if (TYPE_UNSIGNED (type)
2118 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2119 return convert (unsigned_type_node, exp);
2120
2121 return convert (integer_type_node, exp);
2122 }
2123
2124 return exp;
2125 }
2126
2127
2128 /* Perform default promotions for C data used in expressions.
2129 Enumeral types or short or char are converted to int.
2130 In addition, manifest constants symbols are replaced by their values. */
2131
2132 tree
2133 default_conversion (tree exp)
2134 {
2135 tree orig_exp;
2136 tree type = TREE_TYPE (exp);
2137 enum tree_code code = TREE_CODE (type);
2138 tree promoted_type;
2139
2140 mark_exp_read (exp);
2141
2142 /* Functions and arrays have been converted during parsing. */
2143 gcc_assert (code != FUNCTION_TYPE);
2144 if (code == ARRAY_TYPE)
2145 return exp;
2146
2147 /* Constants can be used directly unless they're not loadable. */
2148 if (TREE_CODE (exp) == CONST_DECL)
2149 exp = DECL_INITIAL (exp);
2150
2151 /* Strip no-op conversions. */
2152 orig_exp = exp;
2153 STRIP_TYPE_NOPS (exp);
2154
2155 if (TREE_NO_WARNING (orig_exp))
2156 TREE_NO_WARNING (exp) = 1;
2157
2158 if (code == VOID_TYPE)
2159 {
2160 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2161 "void value not ignored as it ought to be");
2162 return error_mark_node;
2163 }
2164
2165 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2166 if (exp == error_mark_node)
2167 return error_mark_node;
2168
2169 promoted_type = targetm.promoted_type (type);
2170 if (promoted_type)
2171 return convert (promoted_type, exp);
2172
2173 if (INTEGRAL_TYPE_P (type))
2174 return perform_integral_promotions (exp);
2175
2176 return exp;
2177 }
2178 \f
2179 /* Look up COMPONENT in a structure or union TYPE.
2180
2181 If the component name is not found, returns NULL_TREE. Otherwise,
2182 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2183 stepping down the chain to the component, which is in the last
2184 TREE_VALUE of the list. Normally the list is of length one, but if
2185 the component is embedded within (nested) anonymous structures or
2186 unions, the list steps down the chain to the component. */
2187
2188 static tree
2189 lookup_field (tree type, tree component)
2190 {
2191 tree field;
2192
2193 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2194 to the field elements. Use a binary search on this array to quickly
2195 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2196 will always be set for structures which have many elements. */
2197
2198 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2199 {
2200 int bot, top, half;
2201 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2202
2203 field = TYPE_FIELDS (type);
2204 bot = 0;
2205 top = TYPE_LANG_SPECIFIC (type)->s->len;
2206 while (top - bot > 1)
2207 {
2208 half = (top - bot + 1) >> 1;
2209 field = field_array[bot+half];
2210
2211 if (DECL_NAME (field) == NULL_TREE)
2212 {
2213 /* Step through all anon unions in linear fashion. */
2214 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2215 {
2216 field = field_array[bot++];
2217 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2218 {
2219 tree anon = lookup_field (TREE_TYPE (field), component);
2220
2221 if (anon)
2222 return tree_cons (NULL_TREE, field, anon);
2223
2224 /* The Plan 9 compiler permits referring
2225 directly to an anonymous struct/union field
2226 using a typedef name. */
2227 if (flag_plan9_extensions
2228 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2229 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2230 == TYPE_DECL)
2231 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2232 == component))
2233 break;
2234 }
2235 }
2236
2237 /* Entire record is only anon unions. */
2238 if (bot > top)
2239 return NULL_TREE;
2240
2241 /* Restart the binary search, with new lower bound. */
2242 continue;
2243 }
2244
2245 if (DECL_NAME (field) == component)
2246 break;
2247 if (DECL_NAME (field) < component)
2248 bot += half;
2249 else
2250 top = bot + half;
2251 }
2252
2253 if (DECL_NAME (field_array[bot]) == component)
2254 field = field_array[bot];
2255 else if (DECL_NAME (field) != component)
2256 return NULL_TREE;
2257 }
2258 else
2259 {
2260 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2261 {
2262 if (DECL_NAME (field) == NULL_TREE
2263 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2264 {
2265 tree anon = lookup_field (TREE_TYPE (field), component);
2266
2267 if (anon)
2268 return tree_cons (NULL_TREE, field, anon);
2269
2270 /* The Plan 9 compiler permits referring directly to an
2271 anonymous struct/union field using a typedef
2272 name. */
2273 if (flag_plan9_extensions
2274 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2275 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2276 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2277 == component))
2278 break;
2279 }
2280
2281 if (DECL_NAME (field) == component)
2282 break;
2283 }
2284
2285 if (field == NULL_TREE)
2286 return NULL_TREE;
2287 }
2288
2289 return tree_cons (NULL_TREE, field, NULL_TREE);
2290 }
2291
2292 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2293
2294 static void
2295 lookup_field_fuzzy_find_candidates (tree type, tree component,
2296 vec<tree> *candidates)
2297 {
2298 tree field;
2299 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2300 {
2301 if (DECL_NAME (field) == NULL_TREE
2302 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2303 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2304 candidates);
2305
2306 if (DECL_NAME (field))
2307 candidates->safe_push (DECL_NAME (field));
2308 }
2309 }
2310
2311 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2312 rather than returning a TREE_LIST for an exact match. */
2313
2314 static tree
2315 lookup_field_fuzzy (tree type, tree component)
2316 {
2317 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2318
2319 /* First, gather a list of candidates. */
2320 auto_vec <tree> candidates;
2321
2322 lookup_field_fuzzy_find_candidates (type, component,
2323 &candidates);
2324
2325 return find_closest_identifier (component, &candidates);
2326 }
2327
2328 /* Support function for build_component_ref's error-handling.
2329
2330 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2331 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2332
2333 static bool
2334 should_suggest_deref_p (tree datum_type)
2335 {
2336 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2337 allows "." for ptrs; we could be handling a failed attempt
2338 to access a property. */
2339 if (c_dialect_objc ())
2340 return false;
2341
2342 /* Only suggest it for pointers... */
2343 if (TREE_CODE (datum_type) != POINTER_TYPE)
2344 return false;
2345
2346 /* ...to structs/unions. */
2347 tree underlying_type = TREE_TYPE (datum_type);
2348 enum tree_code code = TREE_CODE (underlying_type);
2349 if (code == RECORD_TYPE || code == UNION_TYPE)
2350 return true;
2351 else
2352 return false;
2353 }
2354
2355 /* Make an expression to refer to the COMPONENT field of structure or
2356 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2357 location of the COMPONENT_REF. COMPONENT_LOC is the location
2358 of COMPONENT. */
2359
2360 tree
2361 build_component_ref (location_t loc, tree datum, tree component,
2362 location_t component_loc)
2363 {
2364 tree type = TREE_TYPE (datum);
2365 enum tree_code code = TREE_CODE (type);
2366 tree field = NULL;
2367 tree ref;
2368 bool datum_lvalue = lvalue_p (datum);
2369
2370 if (!objc_is_public (datum, component))
2371 return error_mark_node;
2372
2373 /* Detect Objective-C property syntax object.property. */
2374 if (c_dialect_objc ()
2375 && (ref = objc_maybe_build_component_ref (datum, component)))
2376 return ref;
2377
2378 /* See if there is a field or component with name COMPONENT. */
2379
2380 if (code == RECORD_TYPE || code == UNION_TYPE)
2381 {
2382 if (!COMPLETE_TYPE_P (type))
2383 {
2384 c_incomplete_type_error (loc, NULL_TREE, type);
2385 return error_mark_node;
2386 }
2387
2388 field = lookup_field (type, component);
2389
2390 if (!field)
2391 {
2392 tree guessed_id = lookup_field_fuzzy (type, component);
2393 if (guessed_id)
2394 {
2395 /* Attempt to provide a fixit replacement hint, if
2396 we have a valid range for the component. */
2397 location_t reported_loc
2398 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2399 gcc_rich_location rich_loc (reported_loc);
2400 if (component_loc != UNKNOWN_LOCATION)
2401 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2402 error_at_rich_loc
2403 (&rich_loc,
2404 "%qT has no member named %qE; did you mean %qE?",
2405 type, component, guessed_id);
2406 }
2407 else
2408 error_at (loc, "%qT has no member named %qE", type, component);
2409 return error_mark_node;
2410 }
2411
2412 /* Accessing elements of atomic structures or unions is undefined
2413 behavior (C11 6.5.2.3#5). */
2414 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2415 {
2416 if (code == RECORD_TYPE)
2417 warning_at (loc, 0, "accessing a member %qE of an atomic "
2418 "structure %qE", component, datum);
2419 else
2420 warning_at (loc, 0, "accessing a member %qE of an atomic "
2421 "union %qE", component, datum);
2422 }
2423
2424 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2425 This might be better solved in future the way the C++ front
2426 end does it - by giving the anonymous entities each a
2427 separate name and type, and then have build_component_ref
2428 recursively call itself. We can't do that here. */
2429 do
2430 {
2431 tree subdatum = TREE_VALUE (field);
2432 int quals;
2433 tree subtype;
2434 bool use_datum_quals;
2435
2436 if (TREE_TYPE (subdatum) == error_mark_node)
2437 return error_mark_node;
2438
2439 /* If this is an rvalue, it does not have qualifiers in C
2440 standard terms and we must avoid propagating such
2441 qualifiers down to a non-lvalue array that is then
2442 converted to a pointer. */
2443 use_datum_quals = (datum_lvalue
2444 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2445
2446 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2447 if (use_datum_quals)
2448 quals |= TYPE_QUALS (TREE_TYPE (datum));
2449 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2450
2451 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2452 NULL_TREE);
2453 SET_EXPR_LOCATION (ref, loc);
2454 if (TREE_READONLY (subdatum)
2455 || (use_datum_quals && TREE_READONLY (datum)))
2456 TREE_READONLY (ref) = 1;
2457 if (TREE_THIS_VOLATILE (subdatum)
2458 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2459 TREE_THIS_VOLATILE (ref) = 1;
2460
2461 if (TREE_DEPRECATED (subdatum))
2462 warn_deprecated_use (subdatum, NULL_TREE);
2463
2464 datum = ref;
2465
2466 field = TREE_CHAIN (field);
2467 }
2468 while (field);
2469
2470 return ref;
2471 }
2472 else if (should_suggest_deref_p (type))
2473 {
2474 /* Special-case the error message for "ptr.field" for the case
2475 where the user has confused "." vs "->". */
2476 rich_location richloc (line_table, loc);
2477 /* "loc" should be the "." token. */
2478 richloc.add_fixit_replace ("->");
2479 error_at_rich_loc (&richloc,
2480 "%qE is a pointer; did you mean to use %<->%>?",
2481 datum);
2482 return error_mark_node;
2483 }
2484 else if (code != ERROR_MARK)
2485 error_at (loc,
2486 "request for member %qE in something not a structure or union",
2487 component);
2488
2489 return error_mark_node;
2490 }
2491 \f
2492 /* Given an expression PTR for a pointer, return an expression
2493 for the value pointed to.
2494 ERRORSTRING is the name of the operator to appear in error messages.
2495
2496 LOC is the location to use for the generated tree. */
2497
2498 tree
2499 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2500 {
2501 tree pointer = default_conversion (ptr);
2502 tree type = TREE_TYPE (pointer);
2503 tree ref;
2504
2505 if (TREE_CODE (type) == POINTER_TYPE)
2506 {
2507 if (CONVERT_EXPR_P (pointer)
2508 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2509 {
2510 /* If a warning is issued, mark it to avoid duplicates from
2511 the backend. This only needs to be done at
2512 warn_strict_aliasing > 2. */
2513 if (warn_strict_aliasing > 2)
2514 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2515 type, TREE_OPERAND (pointer, 0)))
2516 TREE_NO_WARNING (pointer) = 1;
2517 }
2518
2519 if (TREE_CODE (pointer) == ADDR_EXPR
2520 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2521 == TREE_TYPE (type)))
2522 {
2523 ref = TREE_OPERAND (pointer, 0);
2524 protected_set_expr_location (ref, loc);
2525 return ref;
2526 }
2527 else
2528 {
2529 tree t = TREE_TYPE (type);
2530
2531 ref = build1 (INDIRECT_REF, t, pointer);
2532
2533 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2534 {
2535 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2536 {
2537 error_at (loc, "dereferencing pointer to incomplete type "
2538 "%qT", t);
2539 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2540 }
2541 return error_mark_node;
2542 }
2543 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2544 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2545
2546 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2547 so that we get the proper error message if the result is used
2548 to assign to. Also, &* is supposed to be a no-op.
2549 And ANSI C seems to specify that the type of the result
2550 should be the const type. */
2551 /* A de-reference of a pointer to const is not a const. It is valid
2552 to change it via some other pointer. */
2553 TREE_READONLY (ref) = TYPE_READONLY (t);
2554 TREE_SIDE_EFFECTS (ref)
2555 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2556 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2557 protected_set_expr_location (ref, loc);
2558 return ref;
2559 }
2560 }
2561 else if (TREE_CODE (pointer) != ERROR_MARK)
2562 invalid_indirection_error (loc, type, errstring);
2563
2564 return error_mark_node;
2565 }
2566
2567 /* This handles expressions of the form "a[i]", which denotes
2568 an array reference.
2569
2570 This is logically equivalent in C to *(a+i), but we may do it differently.
2571 If A is a variable or a member, we generate a primitive ARRAY_REF.
2572 This avoids forcing the array out of registers, and can work on
2573 arrays that are not lvalues (for example, members of structures returned
2574 by functions).
2575
2576 For vector types, allow vector[i] but not i[vector], and create
2577 *(((type*)&vectortype) + i) for the expression.
2578
2579 LOC is the location to use for the returned expression. */
2580
2581 tree
2582 build_array_ref (location_t loc, tree array, tree index)
2583 {
2584 tree ret;
2585 bool swapped = false;
2586 if (TREE_TYPE (array) == error_mark_node
2587 || TREE_TYPE (index) == error_mark_node)
2588 return error_mark_node;
2589
2590 if (flag_cilkplus && contains_array_notation_expr (index))
2591 {
2592 size_t rank = 0;
2593 if (!find_rank (loc, index, index, true, &rank))
2594 return error_mark_node;
2595 if (rank > 1)
2596 {
2597 error_at (loc, "rank of the array's index is greater than 1");
2598 return error_mark_node;
2599 }
2600 }
2601 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2602 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2603 /* Allow vector[index] but not index[vector]. */
2604 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2605 {
2606 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2607 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2608 {
2609 error_at (loc,
2610 "subscripted value is neither array nor pointer nor vector");
2611
2612 return error_mark_node;
2613 }
2614 std::swap (array, index);
2615 swapped = true;
2616 }
2617
2618 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2619 {
2620 error_at (loc, "array subscript is not an integer");
2621 return error_mark_node;
2622 }
2623
2624 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2625 {
2626 error_at (loc, "subscripted value is pointer to function");
2627 return error_mark_node;
2628 }
2629
2630 /* ??? Existing practice has been to warn only when the char
2631 index is syntactically the index, not for char[array]. */
2632 if (!swapped)
2633 warn_array_subscript_with_type_char (loc, index);
2634
2635 /* Apply default promotions *after* noticing character types. */
2636 index = default_conversion (index);
2637 if (index == error_mark_node)
2638 return error_mark_node;
2639
2640 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2641
2642 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2643 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2644
2645 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2646 {
2647 tree rval, type;
2648
2649 /* An array that is indexed by a non-constant
2650 cannot be stored in a register; we must be able to do
2651 address arithmetic on its address.
2652 Likewise an array of elements of variable size. */
2653 if (TREE_CODE (index) != INTEGER_CST
2654 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2655 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2656 {
2657 if (!c_mark_addressable (array))
2658 return error_mark_node;
2659 }
2660 /* An array that is indexed by a constant value which is not within
2661 the array bounds cannot be stored in a register either; because we
2662 would get a crash in store_bit_field/extract_bit_field when trying
2663 to access a non-existent part of the register. */
2664 if (TREE_CODE (index) == INTEGER_CST
2665 && TYPE_DOMAIN (TREE_TYPE (array))
2666 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2667 {
2668 if (!c_mark_addressable (array))
2669 return error_mark_node;
2670 }
2671
2672 if ((pedantic || warn_c90_c99_compat)
2673 && ! was_vector)
2674 {
2675 tree foo = array;
2676 while (TREE_CODE (foo) == COMPONENT_REF)
2677 foo = TREE_OPERAND (foo, 0);
2678 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2679 pedwarn (loc, OPT_Wpedantic,
2680 "ISO C forbids subscripting %<register%> array");
2681 else if (!lvalue_p (foo))
2682 pedwarn_c90 (loc, OPT_Wpedantic,
2683 "ISO C90 forbids subscripting non-lvalue "
2684 "array");
2685 }
2686
2687 type = TREE_TYPE (TREE_TYPE (array));
2688 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2689 /* Array ref is const/volatile if the array elements are
2690 or if the array is. */
2691 TREE_READONLY (rval)
2692 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2693 | TREE_READONLY (array));
2694 TREE_SIDE_EFFECTS (rval)
2695 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2696 | TREE_SIDE_EFFECTS (array));
2697 TREE_THIS_VOLATILE (rval)
2698 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2699 /* This was added by rms on 16 Nov 91.
2700 It fixes vol struct foo *a; a->elts[1]
2701 in an inline function.
2702 Hope it doesn't break something else. */
2703 | TREE_THIS_VOLATILE (array));
2704 ret = require_complete_type (loc, rval);
2705 protected_set_expr_location (ret, loc);
2706 if (non_lvalue)
2707 ret = non_lvalue_loc (loc, ret);
2708 return ret;
2709 }
2710 else
2711 {
2712 tree ar = default_conversion (array);
2713
2714 if (ar == error_mark_node)
2715 return ar;
2716
2717 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2718 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2719
2720 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2721 index, 0),
2722 RO_ARRAY_INDEXING);
2723 if (non_lvalue)
2724 ret = non_lvalue_loc (loc, ret);
2725 return ret;
2726 }
2727 }
2728 \f
2729 /* Build an external reference to identifier ID. FUN indicates
2730 whether this will be used for a function call. LOC is the source
2731 location of the identifier. This sets *TYPE to the type of the
2732 identifier, which is not the same as the type of the returned value
2733 for CONST_DECLs defined as enum constants. If the type of the
2734 identifier is not available, *TYPE is set to NULL. */
2735 tree
2736 build_external_ref (location_t loc, tree id, int fun, tree *type)
2737 {
2738 tree ref;
2739 tree decl = lookup_name (id);
2740
2741 /* In Objective-C, an instance variable (ivar) may be preferred to
2742 whatever lookup_name() found. */
2743 decl = objc_lookup_ivar (decl, id);
2744
2745 *type = NULL;
2746 if (decl && decl != error_mark_node)
2747 {
2748 ref = decl;
2749 *type = TREE_TYPE (ref);
2750 }
2751 else if (fun)
2752 /* Implicit function declaration. */
2753 ref = implicitly_declare (loc, id);
2754 else if (decl == error_mark_node)
2755 /* Don't complain about something that's already been
2756 complained about. */
2757 return error_mark_node;
2758 else
2759 {
2760 undeclared_variable (loc, id);
2761 return error_mark_node;
2762 }
2763
2764 if (TREE_TYPE (ref) == error_mark_node)
2765 return error_mark_node;
2766
2767 if (TREE_DEPRECATED (ref))
2768 warn_deprecated_use (ref, NULL_TREE);
2769
2770 /* Recursive call does not count as usage. */
2771 if (ref != current_function_decl)
2772 {
2773 TREE_USED (ref) = 1;
2774 }
2775
2776 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2777 {
2778 if (!in_sizeof && !in_typeof)
2779 C_DECL_USED (ref) = 1;
2780 else if (DECL_INITIAL (ref) == 0
2781 && DECL_EXTERNAL (ref)
2782 && !TREE_PUBLIC (ref))
2783 record_maybe_used_decl (ref);
2784 }
2785
2786 if (TREE_CODE (ref) == CONST_DECL)
2787 {
2788 used_types_insert (TREE_TYPE (ref));
2789
2790 if (warn_cxx_compat
2791 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2792 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2793 {
2794 warning_at (loc, OPT_Wc___compat,
2795 ("enum constant defined in struct or union "
2796 "is not visible in C++"));
2797 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2798 }
2799
2800 ref = DECL_INITIAL (ref);
2801 TREE_CONSTANT (ref) = 1;
2802 }
2803 else if (current_function_decl != 0
2804 && !DECL_FILE_SCOPE_P (current_function_decl)
2805 && (VAR_OR_FUNCTION_DECL_P (ref)
2806 || TREE_CODE (ref) == PARM_DECL))
2807 {
2808 tree context = decl_function_context (ref);
2809
2810 if (context != 0 && context != current_function_decl)
2811 DECL_NONLOCAL (ref) = 1;
2812 }
2813 /* C99 6.7.4p3: An inline definition of a function with external
2814 linkage ... shall not contain a reference to an identifier with
2815 internal linkage. */
2816 else if (current_function_decl != 0
2817 && DECL_DECLARED_INLINE_P (current_function_decl)
2818 && DECL_EXTERNAL (current_function_decl)
2819 && VAR_OR_FUNCTION_DECL_P (ref)
2820 && (!VAR_P (ref) || TREE_STATIC (ref))
2821 && ! TREE_PUBLIC (ref)
2822 && DECL_CONTEXT (ref) != current_function_decl)
2823 record_inline_static (loc, current_function_decl, ref,
2824 csi_internal);
2825
2826 return ref;
2827 }
2828
2829 /* Record details of decls possibly used inside sizeof or typeof. */
2830 struct maybe_used_decl
2831 {
2832 /* The decl. */
2833 tree decl;
2834 /* The level seen at (in_sizeof + in_typeof). */
2835 int level;
2836 /* The next one at this level or above, or NULL. */
2837 struct maybe_used_decl *next;
2838 };
2839
2840 static struct maybe_used_decl *maybe_used_decls;
2841
2842 /* Record that DECL, an undefined static function reference seen
2843 inside sizeof or typeof, might be used if the operand of sizeof is
2844 a VLA type or the operand of typeof is a variably modified
2845 type. */
2846
2847 static void
2848 record_maybe_used_decl (tree decl)
2849 {
2850 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2851 t->decl = decl;
2852 t->level = in_sizeof + in_typeof;
2853 t->next = maybe_used_decls;
2854 maybe_used_decls = t;
2855 }
2856
2857 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2858 USED is false, just discard them. If it is true, mark them used
2859 (if no longer inside sizeof or typeof) or move them to the next
2860 level up (if still inside sizeof or typeof). */
2861
2862 void
2863 pop_maybe_used (bool used)
2864 {
2865 struct maybe_used_decl *p = maybe_used_decls;
2866 int cur_level = in_sizeof + in_typeof;
2867 while (p && p->level > cur_level)
2868 {
2869 if (used)
2870 {
2871 if (cur_level == 0)
2872 C_DECL_USED (p->decl) = 1;
2873 else
2874 p->level = cur_level;
2875 }
2876 p = p->next;
2877 }
2878 if (!used || cur_level == 0)
2879 maybe_used_decls = p;
2880 }
2881
2882 /* Return the result of sizeof applied to EXPR. */
2883
2884 struct c_expr
2885 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2886 {
2887 struct c_expr ret;
2888 if (expr.value == error_mark_node)
2889 {
2890 ret.value = error_mark_node;
2891 ret.original_code = ERROR_MARK;
2892 ret.original_type = NULL;
2893 pop_maybe_used (false);
2894 }
2895 else
2896 {
2897 bool expr_const_operands = true;
2898
2899 if (TREE_CODE (expr.value) == PARM_DECL
2900 && C_ARRAY_PARAMETER (expr.value))
2901 {
2902 if (warning_at (loc, OPT_Wsizeof_array_argument,
2903 "%<sizeof%> on array function parameter %qE will "
2904 "return size of %qT", expr.value,
2905 expr.original_type))
2906 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2907 }
2908 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2909 &expr_const_operands);
2910 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2911 c_last_sizeof_arg = expr.value;
2912 ret.original_code = SIZEOF_EXPR;
2913 ret.original_type = NULL;
2914 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2915 {
2916 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2917 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2918 folded_expr, ret.value);
2919 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2920 SET_EXPR_LOCATION (ret.value, loc);
2921 }
2922 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2923 }
2924 return ret;
2925 }
2926
2927 /* Return the result of sizeof applied to T, a structure for the type
2928 name passed to sizeof (rather than the type itself). LOC is the
2929 location of the original expression. */
2930
2931 struct c_expr
2932 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2933 {
2934 tree type;
2935 struct c_expr ret;
2936 tree type_expr = NULL_TREE;
2937 bool type_expr_const = true;
2938 type = groktypename (t, &type_expr, &type_expr_const);
2939 ret.value = c_sizeof (loc, type);
2940 c_last_sizeof_arg = type;
2941 ret.original_code = SIZEOF_EXPR;
2942 ret.original_type = NULL;
2943 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2944 && c_vla_type_p (type))
2945 {
2946 /* If the type is a [*] array, it is a VLA but is represented as
2947 having a size of zero. In such a case we must ensure that
2948 the result of sizeof does not get folded to a constant by
2949 c_fully_fold, because if the size is evaluated the result is
2950 not constant and so constraints on zero or negative size
2951 arrays must not be applied when this sizeof call is inside
2952 another array declarator. */
2953 if (!type_expr)
2954 type_expr = integer_zero_node;
2955 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2956 type_expr, ret.value);
2957 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2958 }
2959 pop_maybe_used (type != error_mark_node
2960 ? C_TYPE_VARIABLE_SIZE (type) : false);
2961 return ret;
2962 }
2963
2964 /* Build a function call to function FUNCTION with parameters PARAMS.
2965 The function call is at LOC.
2966 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2967 TREE_VALUE of each node is a parameter-expression.
2968 FUNCTION's data type may be a function type or a pointer-to-function. */
2969
2970 tree
2971 build_function_call (location_t loc, tree function, tree params)
2972 {
2973 vec<tree, va_gc> *v;
2974 tree ret;
2975
2976 vec_alloc (v, list_length (params));
2977 for (; params; params = TREE_CHAIN (params))
2978 v->quick_push (TREE_VALUE (params));
2979 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2980 vec_free (v);
2981 return ret;
2982 }
2983
2984 /* Give a note about the location of the declaration of DECL. */
2985
2986 static void
2987 inform_declaration (tree decl)
2988 {
2989 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2990 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2991 }
2992
2993 /* Build a function call to function FUNCTION with parameters PARAMS.
2994 ORIGTYPES, if not NULL, is a vector of types; each element is
2995 either NULL or the original type of the corresponding element in
2996 PARAMS. The original type may differ from TREE_TYPE of the
2997 parameter for enums. FUNCTION's data type may be a function type
2998 or pointer-to-function. This function changes the elements of
2999 PARAMS. */
3000
3001 tree
3002 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3003 tree function, vec<tree, va_gc> *params,
3004 vec<tree, va_gc> *origtypes)
3005 {
3006 tree fntype, fundecl = 0;
3007 tree name = NULL_TREE, result;
3008 tree tem;
3009 int nargs;
3010 tree *argarray;
3011
3012
3013 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3014 STRIP_TYPE_NOPS (function);
3015
3016 /* Convert anything with function type to a pointer-to-function. */
3017 if (TREE_CODE (function) == FUNCTION_DECL)
3018 {
3019 name = DECL_NAME (function);
3020
3021 if (flag_tm)
3022 tm_malloc_replacement (function);
3023 fundecl = function;
3024 /* Atomic functions have type checking/casting already done. They are
3025 often rewritten and don't match the original parameter list. */
3026 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3027 origtypes = NULL;
3028
3029 if (flag_cilkplus
3030 && is_cilkplus_reduce_builtin (function))
3031 origtypes = NULL;
3032 }
3033 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3034 function = function_to_pointer_conversion (loc, function);
3035
3036 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3037 expressions, like those used for ObjC messenger dispatches. */
3038 if (params && !params->is_empty ())
3039 function = objc_rewrite_function_call (function, (*params)[0]);
3040
3041 function = c_fully_fold (function, false, NULL);
3042
3043 fntype = TREE_TYPE (function);
3044
3045 if (TREE_CODE (fntype) == ERROR_MARK)
3046 return error_mark_node;
3047
3048 if (!(TREE_CODE (fntype) == POINTER_TYPE
3049 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3050 {
3051 if (!flag_diagnostics_show_caret)
3052 error_at (loc,
3053 "called object %qE is not a function or function pointer",
3054 function);
3055 else if (DECL_P (function))
3056 {
3057 error_at (loc,
3058 "called object %qD is not a function or function pointer",
3059 function);
3060 inform_declaration (function);
3061 }
3062 else
3063 error_at (loc,
3064 "called object is not a function or function pointer");
3065 return error_mark_node;
3066 }
3067
3068 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3069 current_function_returns_abnormally = 1;
3070
3071 /* fntype now gets the type of function pointed to. */
3072 fntype = TREE_TYPE (fntype);
3073
3074 /* Convert the parameters to the types declared in the
3075 function prototype, or apply default promotions. */
3076
3077 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3078 origtypes, function, fundecl);
3079 if (nargs < 0)
3080 return error_mark_node;
3081
3082 /* Check that the function is called through a compatible prototype.
3083 If it is not, warn. */
3084 if (CONVERT_EXPR_P (function)
3085 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3086 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3087 && !comptypes (fntype, TREE_TYPE (tem)))
3088 {
3089 tree return_type = TREE_TYPE (fntype);
3090
3091 /* This situation leads to run-time undefined behavior. We can't,
3092 therefore, simply error unless we can prove that all possible
3093 executions of the program must execute the code. */
3094 warning_at (loc, 0, "function called through a non-compatible type");
3095
3096 if (VOID_TYPE_P (return_type)
3097 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3098 pedwarn (loc, 0,
3099 "function with qualified void return type called");
3100 }
3101
3102 argarray = vec_safe_address (params);
3103
3104 /* Check that arguments to builtin functions match the expectations. */
3105 if (fundecl
3106 && DECL_BUILT_IN (fundecl)
3107 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3108 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3109 argarray))
3110 return error_mark_node;
3111
3112 /* Check that the arguments to the function are valid. */
3113 check_function_arguments (loc, fntype, nargs, argarray);
3114
3115 if (name != NULL_TREE
3116 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3117 {
3118 if (require_constant_value)
3119 result =
3120 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3121 function, nargs, argarray);
3122 else
3123 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3124 function, nargs, argarray);
3125 if (TREE_CODE (result) == NOP_EXPR
3126 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3127 STRIP_TYPE_NOPS (result);
3128 }
3129 else
3130 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3131 function, nargs, argarray);
3132
3133 /* In this improbable scenario, a nested function returns a VM type.
3134 Create a TARGET_EXPR so that the call always has a LHS, much as
3135 what the C++ FE does for functions returning non-PODs. */
3136 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3137 {
3138 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3139 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3140 NULL_TREE, NULL_TREE);
3141 }
3142
3143 if (VOID_TYPE_P (TREE_TYPE (result)))
3144 {
3145 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3146 pedwarn (loc, 0,
3147 "function with qualified void return type called");
3148 return result;
3149 }
3150 return require_complete_type (loc, result);
3151 }
3152
3153 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3154
3155 tree
3156 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3157 tree function, vec<tree, va_gc> *params,
3158 vec<tree, va_gc> *origtypes)
3159 {
3160 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3161 STRIP_TYPE_NOPS (function);
3162
3163 /* Convert anything with function type to a pointer-to-function. */
3164 if (TREE_CODE (function) == FUNCTION_DECL)
3165 {
3166 /* Implement type-directed function overloading for builtins.
3167 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3168 handle all the type checking. The result is a complete expression
3169 that implements this function call. */
3170 tree tem = resolve_overloaded_builtin (loc, function, params);
3171 if (tem)
3172 return tem;
3173 }
3174 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3175 }
3176 \f
3177 /* Convert the argument expressions in the vector VALUES
3178 to the types in the list TYPELIST.
3179
3180 If TYPELIST is exhausted, or when an element has NULL as its type,
3181 perform the default conversions.
3182
3183 ORIGTYPES is the original types of the expressions in VALUES. This
3184 holds the type of enum values which have been converted to integral
3185 types. It may be NULL.
3186
3187 FUNCTION is a tree for the called function. It is used only for
3188 error messages, where it is formatted with %qE.
3189
3190 This is also where warnings about wrong number of args are generated.
3191
3192 ARG_LOC are locations of function arguments (if any).
3193
3194 Returns the actual number of arguments processed (which may be less
3195 than the length of VALUES in some error situations), or -1 on
3196 failure. */
3197
3198 static int
3199 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3200 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3201 tree function, tree fundecl)
3202 {
3203 tree typetail, val;
3204 unsigned int parmnum;
3205 bool error_args = false;
3206 const bool type_generic = fundecl
3207 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3208 bool type_generic_remove_excess_precision = false;
3209 bool type_generic_overflow_p = false;
3210 tree selector;
3211
3212 /* Change pointer to function to the function itself for
3213 diagnostics. */
3214 if (TREE_CODE (function) == ADDR_EXPR
3215 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3216 function = TREE_OPERAND (function, 0);
3217
3218 /* Handle an ObjC selector specially for diagnostics. */
3219 selector = objc_message_selector ();
3220
3221 /* For type-generic built-in functions, determine whether excess
3222 precision should be removed (classification) or not
3223 (comparison). */
3224 if (type_generic
3225 && DECL_BUILT_IN (fundecl)
3226 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3227 {
3228 switch (DECL_FUNCTION_CODE (fundecl))
3229 {
3230 case BUILT_IN_ISFINITE:
3231 case BUILT_IN_ISINF:
3232 case BUILT_IN_ISINF_SIGN:
3233 case BUILT_IN_ISNAN:
3234 case BUILT_IN_ISNORMAL:
3235 case BUILT_IN_FPCLASSIFY:
3236 type_generic_remove_excess_precision = true;
3237 break;
3238
3239 case BUILT_IN_ADD_OVERFLOW_P:
3240 case BUILT_IN_SUB_OVERFLOW_P:
3241 case BUILT_IN_MUL_OVERFLOW_P:
3242 /* The last argument of these type-generic builtins
3243 should not be promoted. */
3244 type_generic_overflow_p = true;
3245 break;
3246
3247 default:
3248 break;
3249 }
3250 }
3251 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3252 return vec_safe_length (values);
3253
3254 /* Scan the given expressions and types, producing individual
3255 converted arguments. */
3256
3257 for (typetail = typelist, parmnum = 0;
3258 values && values->iterate (parmnum, &val);
3259 ++parmnum)
3260 {
3261 tree type = typetail ? TREE_VALUE (typetail) : 0;
3262 tree valtype = TREE_TYPE (val);
3263 tree rname = function;
3264 int argnum = parmnum + 1;
3265 const char *invalid_func_diag;
3266 bool excess_precision = false;
3267 bool npc;
3268 tree parmval;
3269 /* Some __atomic_* builtins have additional hidden argument at
3270 position 0. */
3271 location_t ploc
3272 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3273 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3274 : input_location;
3275
3276 if (type == void_type_node)
3277 {
3278 if (selector)
3279 error_at (loc, "too many arguments to method %qE", selector);
3280 else
3281 error_at (loc, "too many arguments to function %qE", function);
3282 inform_declaration (fundecl);
3283 return error_args ? -1 : (int) parmnum;
3284 }
3285
3286 if (selector && argnum > 2)
3287 {
3288 rname = selector;
3289 argnum -= 2;
3290 }
3291
3292 npc = null_pointer_constant_p (val);
3293
3294 /* If there is excess precision and a prototype, convert once to
3295 the required type rather than converting via the semantic
3296 type. Likewise without a prototype a float value represented
3297 as long double should be converted once to double. But for
3298 type-generic classification functions excess precision must
3299 be removed here. */
3300 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3301 && (type || !type_generic || !type_generic_remove_excess_precision))
3302 {
3303 val = TREE_OPERAND (val, 0);
3304 excess_precision = true;
3305 }
3306 val = c_fully_fold (val, false, NULL);
3307 STRIP_TYPE_NOPS (val);
3308
3309 val = require_complete_type (ploc, val);
3310
3311 /* Some floating-point arguments must be promoted to double when
3312 no type is specified by a prototype. This applies to
3313 arguments of type float, and to architecture-specific types
3314 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3315 bool promote_float_arg = false;
3316 if (type == NULL_TREE
3317 && TREE_CODE (valtype) == REAL_TYPE
3318 && (TYPE_PRECISION (valtype)
3319 <= TYPE_PRECISION (double_type_node))
3320 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3321 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3322 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3323 {
3324 /* Promote this argument, unless it has a _FloatN or
3325 _FloatNx type. */
3326 promote_float_arg = true;
3327 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3328 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3329 {
3330 promote_float_arg = false;
3331 break;
3332 }
3333 }
3334
3335 if (type != 0)
3336 {
3337 /* Formal parm type is specified by a function prototype. */
3338
3339 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3340 {
3341 error_at (ploc, "type of formal parameter %d is incomplete",
3342 parmnum + 1);
3343 parmval = val;
3344 }
3345 else
3346 {
3347 tree origtype;
3348
3349 /* Optionally warn about conversions that
3350 differ from the default conversions. */
3351 if (warn_traditional_conversion || warn_traditional)
3352 {
3353 unsigned int formal_prec = TYPE_PRECISION (type);
3354
3355 if (INTEGRAL_TYPE_P (type)
3356 && TREE_CODE (valtype) == REAL_TYPE)
3357 warning_at (ploc, OPT_Wtraditional_conversion,
3358 "passing argument %d of %qE as integer rather "
3359 "than floating due to prototype",
3360 argnum, rname);
3361 if (INTEGRAL_TYPE_P (type)
3362 && TREE_CODE (valtype) == COMPLEX_TYPE)
3363 warning_at (ploc, OPT_Wtraditional_conversion,
3364 "passing argument %d of %qE as integer rather "
3365 "than complex due to prototype",
3366 argnum, rname);
3367 else if (TREE_CODE (type) == COMPLEX_TYPE
3368 && TREE_CODE (valtype) == REAL_TYPE)
3369 warning_at (ploc, OPT_Wtraditional_conversion,
3370 "passing argument %d of %qE as complex rather "
3371 "than floating due to prototype",
3372 argnum, rname);
3373 else if (TREE_CODE (type) == REAL_TYPE
3374 && INTEGRAL_TYPE_P (valtype))
3375 warning_at (ploc, OPT_Wtraditional_conversion,
3376 "passing argument %d of %qE as floating rather "
3377 "than integer due to prototype",
3378 argnum, rname);
3379 else if (TREE_CODE (type) == COMPLEX_TYPE
3380 && INTEGRAL_TYPE_P (valtype))
3381 warning_at (ploc, OPT_Wtraditional_conversion,
3382 "passing argument %d of %qE as complex rather "
3383 "than integer due to prototype",
3384 argnum, rname);
3385 else if (TREE_CODE (type) == REAL_TYPE
3386 && TREE_CODE (valtype) == COMPLEX_TYPE)
3387 warning_at (ploc, OPT_Wtraditional_conversion,
3388 "passing argument %d of %qE as floating rather "
3389 "than complex due to prototype",
3390 argnum, rname);
3391 /* ??? At some point, messages should be written about
3392 conversions between complex types, but that's too messy
3393 to do now. */
3394 else if (TREE_CODE (type) == REAL_TYPE
3395 && TREE_CODE (valtype) == REAL_TYPE)
3396 {
3397 /* Warn if any argument is passed as `float',
3398 since without a prototype it would be `double'. */
3399 if (formal_prec == TYPE_PRECISION (float_type_node)
3400 && type != dfloat32_type_node)
3401 warning_at (ploc, 0,
3402 "passing argument %d of %qE as %<float%> "
3403 "rather than %<double%> due to prototype",
3404 argnum, rname);
3405
3406 /* Warn if mismatch between argument and prototype
3407 for decimal float types. Warn of conversions with
3408 binary float types and of precision narrowing due to
3409 prototype. */
3410 else if (type != valtype
3411 && (type == dfloat32_type_node
3412 || type == dfloat64_type_node
3413 || type == dfloat128_type_node
3414 || valtype == dfloat32_type_node
3415 || valtype == dfloat64_type_node
3416 || valtype == dfloat128_type_node)
3417 && (formal_prec
3418 <= TYPE_PRECISION (valtype)
3419 || (type == dfloat128_type_node
3420 && (valtype
3421 != dfloat64_type_node
3422 && (valtype
3423 != dfloat32_type_node)))
3424 || (type == dfloat64_type_node
3425 && (valtype
3426 != dfloat32_type_node))))
3427 warning_at (ploc, 0,
3428 "passing argument %d of %qE as %qT "
3429 "rather than %qT due to prototype",
3430 argnum, rname, type, valtype);
3431
3432 }
3433 /* Detect integer changing in width or signedness.
3434 These warnings are only activated with
3435 -Wtraditional-conversion, not with -Wtraditional. */
3436 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3437 && INTEGRAL_TYPE_P (valtype))
3438 {
3439 tree would_have_been = default_conversion (val);
3440 tree type1 = TREE_TYPE (would_have_been);
3441
3442 if (TREE_CODE (type) == ENUMERAL_TYPE
3443 && (TYPE_MAIN_VARIANT (type)
3444 == TYPE_MAIN_VARIANT (valtype)))
3445 /* No warning if function asks for enum
3446 and the actual arg is that enum type. */
3447 ;
3448 else if (formal_prec != TYPE_PRECISION (type1))
3449 warning_at (ploc, OPT_Wtraditional_conversion,
3450 "passing argument %d of %qE "
3451 "with different width due to prototype",
3452 argnum, rname);
3453 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3454 ;
3455 /* Don't complain if the formal parameter type
3456 is an enum, because we can't tell now whether
3457 the value was an enum--even the same enum. */
3458 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3459 ;
3460 else if (TREE_CODE (val) == INTEGER_CST
3461 && int_fits_type_p (val, type))
3462 /* Change in signedness doesn't matter
3463 if a constant value is unaffected. */
3464 ;
3465 /* If the value is extended from a narrower
3466 unsigned type, it doesn't matter whether we
3467 pass it as signed or unsigned; the value
3468 certainly is the same either way. */
3469 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3470 && TYPE_UNSIGNED (valtype))
3471 ;
3472 else if (TYPE_UNSIGNED (type))
3473 warning_at (ploc, OPT_Wtraditional_conversion,
3474 "passing argument %d of %qE "
3475 "as unsigned due to prototype",
3476 argnum, rname);
3477 else
3478 warning_at (ploc, OPT_Wtraditional_conversion,
3479 "passing argument %d of %qE "
3480 "as signed due to prototype",
3481 argnum, rname);
3482 }
3483 }
3484
3485 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3486 sake of better warnings from convert_and_check. */
3487 if (excess_precision)
3488 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3489 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3490 parmval = convert_for_assignment (loc, ploc, type,
3491 val, origtype, ic_argpass,
3492 npc, fundecl, function,
3493 parmnum + 1);
3494
3495 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3496 && INTEGRAL_TYPE_P (type)
3497 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3498 parmval = default_conversion (parmval);
3499 }
3500 }
3501 else if (promote_float_arg)
3502 {
3503 if (type_generic)
3504 parmval = val;
3505 else
3506 {
3507 /* Convert `float' to `double'. */
3508 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3509 warning_at (ploc, OPT_Wdouble_promotion,
3510 "implicit conversion from %qT to %qT when passing "
3511 "argument to function",
3512 valtype, double_type_node);
3513 parmval = convert (double_type_node, val);
3514 }
3515 }
3516 else if ((excess_precision && !type_generic)
3517 || (type_generic_overflow_p && parmnum == 2))
3518 /* A "double" argument with excess precision being passed
3519 without a prototype or in variable arguments.
3520 The last argument of __builtin_*_overflow_p should not be
3521 promoted. */
3522 parmval = convert (valtype, val);
3523 else if ((invalid_func_diag =
3524 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3525 {
3526 error (invalid_func_diag);
3527 return -1;
3528 }
3529 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3530 {
3531 return -1;
3532 }
3533 else
3534 /* Convert `short' and `char' to full-size `int'. */
3535 parmval = default_conversion (val);
3536
3537 (*values)[parmnum] = parmval;
3538 if (parmval == error_mark_node)
3539 error_args = true;
3540
3541 if (typetail)
3542 typetail = TREE_CHAIN (typetail);
3543 }
3544
3545 gcc_assert (parmnum == vec_safe_length (values));
3546
3547 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3548 {
3549 error_at (loc, "too few arguments to function %qE", function);
3550 inform_declaration (fundecl);
3551 return -1;
3552 }
3553
3554 return error_args ? -1 : (int) parmnum;
3555 }
3556 \f
3557 /* This is the entry point used by the parser to build unary operators
3558 in the input. CODE, a tree_code, specifies the unary operator, and
3559 ARG is the operand. For unary plus, the C parser currently uses
3560 CONVERT_EXPR for code.
3561
3562 LOC is the location to use for the tree generated.
3563 */
3564
3565 struct c_expr
3566 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3567 {
3568 struct c_expr result;
3569
3570 result.original_code = code;
3571 result.original_type = NULL;
3572
3573 if (reject_gcc_builtin (arg.value))
3574 {
3575 result.value = error_mark_node;
3576 }
3577 else
3578 {
3579 result.value = build_unary_op (loc, code, arg.value, false);
3580
3581 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3582 overflow_warning (loc, result.value);
3583 }
3584
3585 /* We are typically called when parsing a prefix token at LOC acting on
3586 ARG. Reflect this by updating the source range of the result to
3587 start at LOC and end at the end of ARG. */
3588 set_c_expr_source_range (&result,
3589 loc, arg.get_finish ());
3590
3591 return result;
3592 }
3593
3594 /* This is the entry point used by the parser to build binary operators
3595 in the input. CODE, a tree_code, specifies the binary operator, and
3596 ARG1 and ARG2 are the operands. In addition to constructing the
3597 expression, we check for operands that were written with other binary
3598 operators in a way that is likely to confuse the user.
3599
3600 LOCATION is the location of the binary operator. */
3601
3602 struct c_expr
3603 parser_build_binary_op (location_t location, enum tree_code code,
3604 struct c_expr arg1, struct c_expr arg2)
3605 {
3606 struct c_expr result;
3607
3608 enum tree_code code1 = arg1.original_code;
3609 enum tree_code code2 = arg2.original_code;
3610 tree type1 = (arg1.original_type
3611 ? arg1.original_type
3612 : TREE_TYPE (arg1.value));
3613 tree type2 = (arg2.original_type
3614 ? arg2.original_type
3615 : TREE_TYPE (arg2.value));
3616
3617 result.value = build_binary_op (location, code,
3618 arg1.value, arg2.value, 1);
3619 result.original_code = code;
3620 result.original_type = NULL;
3621
3622 if (TREE_CODE (result.value) == ERROR_MARK)
3623 {
3624 set_c_expr_source_range (&result,
3625 arg1.get_start (),
3626 arg2.get_finish ());
3627 return result;
3628 }
3629
3630 if (location != UNKNOWN_LOCATION)
3631 protected_set_expr_location (result.value, location);
3632
3633 set_c_expr_source_range (&result,
3634 arg1.get_start (),
3635 arg2.get_finish ());
3636
3637 /* Check for cases such as x+y<<z which users are likely
3638 to misinterpret. */
3639 if (warn_parentheses)
3640 warn_about_parentheses (location, code, code1, arg1.value, code2,
3641 arg2.value);
3642
3643 if (warn_logical_op)
3644 warn_logical_operator (location, code, TREE_TYPE (result.value),
3645 code1, arg1.value, code2, arg2.value);
3646
3647 if (warn_tautological_compare)
3648 {
3649 tree lhs = arg1.value;
3650 tree rhs = arg2.value;
3651 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3652 {
3653 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3654 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3655 lhs = NULL_TREE;
3656 else
3657 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3658 }
3659 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3660 {
3661 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3662 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3663 rhs = NULL_TREE;
3664 else
3665 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3666 }
3667 if (lhs != NULL_TREE && rhs != NULL_TREE)
3668 warn_tautological_cmp (location, code, lhs, rhs);
3669 }
3670
3671 if (warn_logical_not_paren
3672 && TREE_CODE_CLASS (code) == tcc_comparison
3673 && code1 == TRUTH_NOT_EXPR
3674 && code2 != TRUTH_NOT_EXPR
3675 /* Avoid warning for !!x == y. */
3676 && (TREE_CODE (arg1.value) != NE_EXPR
3677 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3678 {
3679 /* Avoid warning for !b == y where b has _Bool type. */
3680 tree t = integer_zero_node;
3681 if (TREE_CODE (arg1.value) == EQ_EXPR
3682 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3683 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3684 {
3685 t = TREE_OPERAND (arg1.value, 0);
3686 do
3687 {
3688 if (TREE_TYPE (t) != integer_type_node)
3689 break;
3690 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3691 t = C_MAYBE_CONST_EXPR_EXPR (t);
3692 else if (CONVERT_EXPR_P (t))
3693 t = TREE_OPERAND (t, 0);
3694 else
3695 break;
3696 }
3697 while (1);
3698 }
3699 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3700 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3701 }
3702
3703 /* Warn about comparisons against string literals, with the exception
3704 of testing for equality or inequality of a string literal with NULL. */
3705 if (code == EQ_EXPR || code == NE_EXPR)
3706 {
3707 if ((code1 == STRING_CST
3708 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3709 || (code2 == STRING_CST
3710 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3711 warning_at (location, OPT_Waddress,
3712 "comparison with string literal results in unspecified behavior");
3713 }
3714 else if (TREE_CODE_CLASS (code) == tcc_comparison
3715 && (code1 == STRING_CST || code2 == STRING_CST))
3716 warning_at (location, OPT_Waddress,
3717 "comparison with string literal results in unspecified behavior");
3718
3719 if (TREE_OVERFLOW_P (result.value)
3720 && !TREE_OVERFLOW_P (arg1.value)
3721 && !TREE_OVERFLOW_P (arg2.value))
3722 overflow_warning (location, result.value);
3723
3724 /* Warn about comparisons of different enum types. */
3725 if (warn_enum_compare
3726 && TREE_CODE_CLASS (code) == tcc_comparison
3727 && TREE_CODE (type1) == ENUMERAL_TYPE
3728 && TREE_CODE (type2) == ENUMERAL_TYPE
3729 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3730 warning_at (location, OPT_Wenum_compare,
3731 "comparison between %qT and %qT",
3732 type1, type2);
3733
3734 return result;
3735 }
3736 \f
3737 /* Return a tree for the difference of pointers OP0 and OP1.
3738 The resulting tree has type int. */
3739
3740 static tree
3741 pointer_diff (location_t loc, tree op0, tree op1)
3742 {
3743 tree restype = ptrdiff_type_node;
3744 tree result, inttype;
3745
3746 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3747 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3748 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3749 tree orig_op1 = op1;
3750
3751 /* If the operands point into different address spaces, we need to
3752 explicitly convert them to pointers into the common address space
3753 before we can subtract the numerical address values. */
3754 if (as0 != as1)
3755 {
3756 addr_space_t as_common;
3757 tree common_type;
3758
3759 /* Determine the common superset address space. This is guaranteed
3760 to exist because the caller verified that comp_target_types
3761 returned non-zero. */
3762 if (!addr_space_superset (as0, as1, &as_common))
3763 gcc_unreachable ();
3764
3765 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3766 op0 = convert (common_type, op0);
3767 op1 = convert (common_type, op1);
3768 }
3769
3770 /* Determine integer type to perform computations in. This will usually
3771 be the same as the result type (ptrdiff_t), but may need to be a wider
3772 type if pointers for the address space are wider than ptrdiff_t. */
3773 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3774 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3775 else
3776 inttype = restype;
3777
3778 if (TREE_CODE (target_type) == VOID_TYPE)
3779 pedwarn (loc, OPT_Wpointer_arith,
3780 "pointer of type %<void *%> used in subtraction");
3781 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3782 pedwarn (loc, OPT_Wpointer_arith,
3783 "pointer to a function used in subtraction");
3784
3785 /* First do the subtraction as integers;
3786 then drop through to build the divide operator.
3787 Do not do default conversions on the minus operator
3788 in case restype is a short type. */
3789
3790 op0 = build_binary_op (loc,
3791 MINUS_EXPR, convert (inttype, op0),
3792 convert (inttype, op1), 0);
3793 /* This generates an error if op1 is pointer to incomplete type. */
3794 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3795 error_at (loc, "arithmetic on pointer to an incomplete type");
3796
3797 op1 = c_size_in_bytes (target_type);
3798
3799 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3800 error_at (loc, "arithmetic on pointer to an empty aggregate");
3801
3802 /* Divide by the size, in easiest possible way. */
3803 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3804 op0, convert (inttype, op1));
3805
3806 /* Convert to final result type if necessary. */
3807 return convert (restype, result);
3808 }
3809 \f
3810 /* Expand atomic compound assignments into an appropriate sequence as
3811 specified by the C11 standard section 6.5.16.2.
3812
3813 _Atomic T1 E1
3814 T2 E2
3815 E1 op= E2
3816
3817 This sequence is used for all types for which these operations are
3818 supported.
3819
3820 In addition, built-in versions of the 'fe' prefixed routines may
3821 need to be invoked for floating point (real, complex or vector) when
3822 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3823
3824 T1 newval;
3825 T1 old;
3826 T1 *addr
3827 T2 val
3828 fenv_t fenv
3829
3830 addr = &E1;
3831 val = (E2);
3832 __atomic_load (addr, &old, SEQ_CST);
3833 feholdexcept (&fenv);
3834 loop:
3835 newval = old op val;
3836 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3837 SEQ_CST))
3838 goto done;
3839 feclearexcept (FE_ALL_EXCEPT);
3840 goto loop:
3841 done:
3842 feupdateenv (&fenv);
3843
3844 The compiler will issue the __atomic_fetch_* built-in when possible,
3845 otherwise it will generate the generic form of the atomic operations.
3846 This requires temp(s) and has their address taken. The atomic processing
3847 is smart enough to figure out when the size of an object can utilize
3848 a lock-free version, and convert the built-in call to the appropriate
3849 lock-free routine. The optimizers will then dispose of any temps that
3850 are no longer required, and lock-free implementations are utilized as
3851 long as there is target support for the required size.
3852
3853 If the operator is NOP_EXPR, then this is a simple assignment, and
3854 an __atomic_store is issued to perform the assignment rather than
3855 the above loop. */
3856
3857 /* Build an atomic assignment at LOC, expanding into the proper
3858 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3859 the result of the operation, unless RETURN_OLD_P, in which case
3860 return the old value of LHS (this is only for postincrement and
3861 postdecrement). */
3862
3863 static tree
3864 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3865 tree rhs, bool return_old_p)
3866 {
3867 tree fndecl, func_call;
3868 vec<tree, va_gc> *params;
3869 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3870 tree old, old_addr;
3871 tree compound_stmt;
3872 tree stmt, goto_stmt;
3873 tree loop_label, loop_decl, done_label, done_decl;
3874
3875 tree lhs_type = TREE_TYPE (lhs);
3876 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
3877 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3878 tree rhs_type = TREE_TYPE (rhs);
3879
3880 gcc_assert (TYPE_ATOMIC (lhs_type));
3881
3882 if (return_old_p)
3883 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3884
3885 /* Allocate enough vector items for a compare_exchange. */
3886 vec_alloc (params, 6);
3887
3888 /* Create a compound statement to hold the sequence of statements
3889 with a loop. */
3890 compound_stmt = c_begin_compound_stmt (false);
3891
3892 /* Fold the RHS if it hasn't already been folded. */
3893 if (modifycode != NOP_EXPR)
3894 rhs = c_fully_fold (rhs, false, NULL);
3895
3896 /* Remove the qualifiers for the rest of the expressions and create
3897 the VAL temp variable to hold the RHS. */
3898 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3899 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3900 val = create_tmp_var_raw (nonatomic_rhs_type);
3901 TREE_ADDRESSABLE (val) = 1;
3902 TREE_NO_WARNING (val) = 1;
3903 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3904 NULL_TREE);
3905 SET_EXPR_LOCATION (rhs, loc);
3906 add_stmt (rhs);
3907
3908 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3909 an atomic_store. */
3910 if (modifycode == NOP_EXPR)
3911 {
3912 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3913 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
3914 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3915 params->quick_push (lhs_addr);
3916 params->quick_push (rhs);
3917 params->quick_push (seq_cst);
3918 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3919 add_stmt (func_call);
3920
3921 /* Finish the compound statement. */
3922 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3923
3924 /* VAL is the value which was stored, return a COMPOUND_STMT of
3925 the statement and that value. */
3926 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3927 }
3928
3929 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3930 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3931 isn't applicable for such builtins. ??? Do we want to handle enums? */
3932 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3933 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3934 {
3935 built_in_function fncode;
3936 switch (modifycode)
3937 {
3938 case PLUS_EXPR:
3939 case POINTER_PLUS_EXPR:
3940 fncode = (return_old_p
3941 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3942 : BUILT_IN_ATOMIC_ADD_FETCH_N);
3943 break;
3944 case MINUS_EXPR:
3945 fncode = (return_old_p
3946 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3947 : BUILT_IN_ATOMIC_SUB_FETCH_N);
3948 break;
3949 case BIT_AND_EXPR:
3950 fncode = (return_old_p
3951 ? BUILT_IN_ATOMIC_FETCH_AND_N
3952 : BUILT_IN_ATOMIC_AND_FETCH_N);
3953 break;
3954 case BIT_IOR_EXPR:
3955 fncode = (return_old_p
3956 ? BUILT_IN_ATOMIC_FETCH_OR_N
3957 : BUILT_IN_ATOMIC_OR_FETCH_N);
3958 break;
3959 case BIT_XOR_EXPR:
3960 fncode = (return_old_p
3961 ? BUILT_IN_ATOMIC_FETCH_XOR_N
3962 : BUILT_IN_ATOMIC_XOR_FETCH_N);
3963 break;
3964 default:
3965 goto cas_loop;
3966 }
3967
3968 /* We can only use "_1" through "_16" variants of the atomic fetch
3969 built-ins. */
3970 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
3971 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
3972 goto cas_loop;
3973
3974 /* If this is a pointer type, we need to multiply by the size of
3975 the pointer target type. */
3976 if (POINTER_TYPE_P (lhs_type))
3977 {
3978 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
3979 /* ??? This would introduce -Wdiscarded-qualifiers
3980 warning: __atomic_fetch_* expect volatile void *
3981 type as the first argument. (Assignments between
3982 atomic and non-atomic objects are OK.) */
3983 || TYPE_RESTRICT (lhs_type))
3984 goto cas_loop;
3985 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
3986 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
3987 convert (ptrdiff_type_node, rhs),
3988 convert (ptrdiff_type_node, sz));
3989 }
3990
3991 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
3992 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
3993 fndecl = builtin_decl_explicit (fncode);
3994 params->quick_push (lhs_addr);
3995 params->quick_push (rhs);
3996 params->quick_push (seq_cst);
3997 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3998
3999 newval = create_tmp_var_raw (nonatomic_lhs_type);
4000 TREE_ADDRESSABLE (newval) = 1;
4001 TREE_NO_WARNING (newval) = 1;
4002 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4003 NULL_TREE, NULL_TREE);
4004 SET_EXPR_LOCATION (rhs, loc);
4005 add_stmt (rhs);
4006
4007 /* Finish the compound statement. */
4008 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4009
4010 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4011 the statement and that value. */
4012 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4013 }
4014
4015 cas_loop:
4016 /* Create the variables and labels required for the op= form. */
4017 old = create_tmp_var_raw (nonatomic_lhs_type);
4018 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4019 TREE_ADDRESSABLE (old) = 1;
4020 TREE_NO_WARNING (old) = 1;
4021
4022 newval = create_tmp_var_raw (nonatomic_lhs_type);
4023 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4024 TREE_ADDRESSABLE (newval) = 1;
4025 TREE_NO_WARNING (newval) = 1;
4026
4027 loop_decl = create_artificial_label (loc);
4028 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4029
4030 done_decl = create_artificial_label (loc);
4031 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4032
4033 /* __atomic_load (addr, &old, SEQ_CST). */
4034 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4035 params->quick_push (lhs_addr);
4036 params->quick_push (old_addr);
4037 params->quick_push (seq_cst);
4038 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4039 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4040 NULL_TREE);
4041 add_stmt (old);
4042 params->truncate (0);
4043
4044 /* Create the expressions for floating-point environment
4045 manipulation, if required. */
4046 bool need_fenv = (flag_trapping_math
4047 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4048 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4049 if (need_fenv)
4050 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4051
4052 if (hold_call)
4053 add_stmt (hold_call);
4054
4055 /* loop: */
4056 add_stmt (loop_label);
4057
4058 /* newval = old + val; */
4059 rhs = build_binary_op (loc, modifycode, old, val, 1);
4060 rhs = c_fully_fold (rhs, false, NULL);
4061 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4062 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4063 NULL_TREE, 0);
4064 if (rhs != error_mark_node)
4065 {
4066 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4067 NULL_TREE);
4068 SET_EXPR_LOCATION (rhs, loc);
4069 add_stmt (rhs);
4070 }
4071
4072 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4073 goto done; */
4074 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4075 params->quick_push (lhs_addr);
4076 params->quick_push (old_addr);
4077 params->quick_push (newval_addr);
4078 params->quick_push (integer_zero_node);
4079 params->quick_push (seq_cst);
4080 params->quick_push (seq_cst);
4081 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4082
4083 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4084 SET_EXPR_LOCATION (goto_stmt, loc);
4085
4086 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4087 SET_EXPR_LOCATION (stmt, loc);
4088 add_stmt (stmt);
4089
4090 if (clear_call)
4091 add_stmt (clear_call);
4092
4093 /* goto loop; */
4094 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4095 SET_EXPR_LOCATION (goto_stmt, loc);
4096 add_stmt (goto_stmt);
4097
4098 /* done: */
4099 add_stmt (done_label);
4100
4101 if (update_call)
4102 add_stmt (update_call);
4103
4104 /* Finish the compound statement. */
4105 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4106
4107 /* NEWVAL is the value that was successfully stored, return a
4108 COMPOUND_EXPR of the statement and the appropriate value. */
4109 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4110 return_old_p ? old : newval);
4111 }
4112
4113 /* Construct and perhaps optimize a tree representation
4114 for a unary operation. CODE, a tree_code, specifies the operation
4115 and XARG is the operand.
4116 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4117 promotions (such as from short to int).
4118 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4119 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4120 to pointers in C99.
4121
4122 LOCATION is the location of the operator. */
4123
4124 tree
4125 build_unary_op (location_t location, enum tree_code code, tree xarg,
4126 bool noconvert)
4127 {
4128 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4129 tree arg = xarg;
4130 tree argtype = 0;
4131 enum tree_code typecode;
4132 tree val;
4133 tree ret = error_mark_node;
4134 tree eptype = NULL_TREE;
4135 const char *invalid_op_diag;
4136 bool int_operands;
4137
4138 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4139 if (int_operands)
4140 arg = remove_c_maybe_const_expr (arg);
4141
4142 if (code != ADDR_EXPR)
4143 arg = require_complete_type (location, arg);
4144
4145 typecode = TREE_CODE (TREE_TYPE (arg));
4146 if (typecode == ERROR_MARK)
4147 return error_mark_node;
4148 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4149 typecode = INTEGER_TYPE;
4150
4151 if ((invalid_op_diag
4152 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4153 {
4154 error_at (location, invalid_op_diag);
4155 return error_mark_node;
4156 }
4157
4158 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4159 {
4160 eptype = TREE_TYPE (arg);
4161 arg = TREE_OPERAND (arg, 0);
4162 }
4163
4164 switch (code)
4165 {
4166 case CONVERT_EXPR:
4167 /* This is used for unary plus, because a CONVERT_EXPR
4168 is enough to prevent anybody from looking inside for
4169 associativity, but won't generate any code. */
4170 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4171 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4172 || typecode == VECTOR_TYPE))
4173 {
4174 error_at (location, "wrong type argument to unary plus");
4175 return error_mark_node;
4176 }
4177 else if (!noconvert)
4178 arg = default_conversion (arg);
4179 arg = non_lvalue_loc (location, arg);
4180 break;
4181
4182 case NEGATE_EXPR:
4183 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4184 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4185 || typecode == VECTOR_TYPE))
4186 {
4187 error_at (location, "wrong type argument to unary minus");
4188 return error_mark_node;
4189 }
4190 else if (!noconvert)
4191 arg = default_conversion (arg);
4192 break;
4193
4194 case BIT_NOT_EXPR:
4195 /* ~ works on integer types and non float vectors. */
4196 if (typecode == INTEGER_TYPE
4197 || (typecode == VECTOR_TYPE
4198 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4199 {
4200 tree e = arg;
4201
4202 /* Warn if the expression has boolean value. */
4203 while (TREE_CODE (e) == COMPOUND_EXPR)
4204 e = TREE_OPERAND (e, 1);
4205
4206 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4207 || truth_value_p (TREE_CODE (e)))
4208 && warning_at (location, OPT_Wbool_operation,
4209 "%<~%> on a boolean expression"))
4210 {
4211 gcc_rich_location richloc (location);
4212 richloc.add_fixit_insert_before (location, "!");
4213 inform_at_rich_loc (&richloc, "did you mean to use logical "
4214 "not?");
4215 }
4216 if (!noconvert)
4217 arg = default_conversion (arg);
4218 }
4219 else if (typecode == COMPLEX_TYPE)
4220 {
4221 code = CONJ_EXPR;
4222 pedwarn (location, OPT_Wpedantic,
4223 "ISO C does not support %<~%> for complex conjugation");
4224 if (!noconvert)
4225 arg = default_conversion (arg);
4226 }
4227 else
4228 {
4229 error_at (location, "wrong type argument to bit-complement");
4230 return error_mark_node;
4231 }
4232 break;
4233
4234 case ABS_EXPR:
4235 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4236 {
4237 error_at (location, "wrong type argument to abs");
4238 return error_mark_node;
4239 }
4240 else if (!noconvert)
4241 arg = default_conversion (arg);
4242 break;
4243
4244 case CONJ_EXPR:
4245 /* Conjugating a real value is a no-op, but allow it anyway. */
4246 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4247 || typecode == COMPLEX_TYPE))
4248 {
4249 error_at (location, "wrong type argument to conjugation");
4250 return error_mark_node;
4251 }
4252 else if (!noconvert)
4253 arg = default_conversion (arg);
4254 break;
4255
4256 case TRUTH_NOT_EXPR:
4257 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4258 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4259 && typecode != COMPLEX_TYPE)
4260 {
4261 error_at (location,
4262 "wrong type argument to unary exclamation mark");
4263 return error_mark_node;
4264 }
4265 if (int_operands)
4266 {
4267 arg = c_objc_common_truthvalue_conversion (location, xarg);
4268 arg = remove_c_maybe_const_expr (arg);
4269 }
4270 else
4271 arg = c_objc_common_truthvalue_conversion (location, arg);
4272 ret = invert_truthvalue_loc (location, arg);
4273 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4274 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4275 location = EXPR_LOCATION (ret);
4276 goto return_build_unary_op;
4277
4278 case REALPART_EXPR:
4279 case IMAGPART_EXPR:
4280 ret = build_real_imag_expr (location, code, arg);
4281 if (ret == error_mark_node)
4282 return error_mark_node;
4283 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4284 eptype = TREE_TYPE (eptype);
4285 goto return_build_unary_op;
4286
4287 case PREINCREMENT_EXPR:
4288 case POSTINCREMENT_EXPR:
4289 case PREDECREMENT_EXPR:
4290 case POSTDECREMENT_EXPR:
4291
4292 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4293 {
4294 tree inner = build_unary_op (location, code,
4295 C_MAYBE_CONST_EXPR_EXPR (arg),
4296 noconvert);
4297 if (inner == error_mark_node)
4298 return error_mark_node;
4299 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4300 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4301 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4302 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4303 goto return_build_unary_op;
4304 }
4305
4306 /* Complain about anything that is not a true lvalue. In
4307 Objective-C, skip this check for property_refs. */
4308 if (!objc_is_property_ref (arg)
4309 && !lvalue_or_else (location,
4310 arg, ((code == PREINCREMENT_EXPR
4311 || code == POSTINCREMENT_EXPR)
4312 ? lv_increment
4313 : lv_decrement)))
4314 return error_mark_node;
4315
4316 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4317 {
4318 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4319 warning_at (location, OPT_Wc___compat,
4320 "increment of enumeration value is invalid in C++");
4321 else
4322 warning_at (location, OPT_Wc___compat,
4323 "decrement of enumeration value is invalid in C++");
4324 }
4325
4326 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4327 {
4328 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4329 warning_at (location, OPT_Wbool_operation,
4330 "increment of a boolean expression");
4331 else
4332 warning_at (location, OPT_Wbool_operation,
4333 "decrement of a boolean expression");
4334 }
4335
4336 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4337 arg = c_fully_fold (arg, false, NULL);
4338
4339 bool atomic_op;
4340 atomic_op = really_atomic_lvalue (arg);
4341
4342 /* Increment or decrement the real part of the value,
4343 and don't change the imaginary part. */
4344 if (typecode == COMPLEX_TYPE)
4345 {
4346 tree real, imag;
4347
4348 pedwarn (location, OPT_Wpedantic,
4349 "ISO C does not support %<++%> and %<--%> on complex types");
4350
4351 if (!atomic_op)
4352 {
4353 arg = stabilize_reference (arg);
4354 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4355 true);
4356 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4357 true);
4358 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4359 if (real == error_mark_node || imag == error_mark_node)
4360 return error_mark_node;
4361 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4362 real, imag);
4363 goto return_build_unary_op;
4364 }
4365 }
4366
4367 /* Report invalid types. */
4368
4369 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4370 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4371 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4372 {
4373 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4374 error_at (location, "wrong type argument to increment");
4375 else
4376 error_at (location, "wrong type argument to decrement");
4377
4378 return error_mark_node;
4379 }
4380
4381 {
4382 tree inc;
4383
4384 argtype = TREE_TYPE (arg);
4385
4386 /* Compute the increment. */
4387
4388 if (typecode == POINTER_TYPE)
4389 {
4390 /* If pointer target is an incomplete type,
4391 we just cannot know how to do the arithmetic. */
4392 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4393 {
4394 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4395 error_at (location,
4396 "increment of pointer to an incomplete type %qT",
4397 TREE_TYPE (argtype));
4398 else
4399 error_at (location,
4400 "decrement of pointer to an incomplete type %qT",
4401 TREE_TYPE (argtype));
4402 }
4403 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4404 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4405 {
4406 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4407 pedwarn (location, OPT_Wpointer_arith,
4408 "wrong type argument to increment");
4409 else
4410 pedwarn (location, OPT_Wpointer_arith,
4411 "wrong type argument to decrement");
4412 }
4413
4414 inc = c_size_in_bytes (TREE_TYPE (argtype));
4415 inc = convert_to_ptrofftype_loc (location, inc);
4416 }
4417 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4418 {
4419 /* For signed fract types, we invert ++ to -- or
4420 -- to ++, and change inc from 1 to -1, because
4421 it is not possible to represent 1 in signed fract constants.
4422 For unsigned fract types, the result always overflows and
4423 we get an undefined (original) or the maximum value. */
4424 if (code == PREINCREMENT_EXPR)
4425 code = PREDECREMENT_EXPR;
4426 else if (code == PREDECREMENT_EXPR)
4427 code = PREINCREMENT_EXPR;
4428 else if (code == POSTINCREMENT_EXPR)
4429 code = POSTDECREMENT_EXPR;
4430 else /* code == POSTDECREMENT_EXPR */
4431 code = POSTINCREMENT_EXPR;
4432
4433 inc = integer_minus_one_node;
4434 inc = convert (argtype, inc);
4435 }
4436 else
4437 {
4438 inc = VECTOR_TYPE_P (argtype)
4439 ? build_one_cst (argtype)
4440 : integer_one_node;
4441 inc = convert (argtype, inc);
4442 }
4443
4444 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4445 need to ask Objective-C to build the increment or decrement
4446 expression for it. */
4447 if (objc_is_property_ref (arg))
4448 return objc_build_incr_expr_for_property_ref (location, code,
4449 arg, inc);
4450
4451 /* Report a read-only lvalue. */
4452 if (TYPE_READONLY (argtype))
4453 {
4454 readonly_error (location, arg,
4455 ((code == PREINCREMENT_EXPR
4456 || code == POSTINCREMENT_EXPR)
4457 ? lv_increment : lv_decrement));
4458 return error_mark_node;
4459 }
4460 else if (TREE_READONLY (arg))
4461 readonly_warning (arg,
4462 ((code == PREINCREMENT_EXPR
4463 || code == POSTINCREMENT_EXPR)
4464 ? lv_increment : lv_decrement));
4465
4466 /* If the argument is atomic, use the special code sequences for
4467 atomic compound assignment. */
4468 if (atomic_op)
4469 {
4470 arg = stabilize_reference (arg);
4471 ret = build_atomic_assign (location, arg,
4472 ((code == PREINCREMENT_EXPR
4473 || code == POSTINCREMENT_EXPR)
4474 ? PLUS_EXPR
4475 : MINUS_EXPR),
4476 (FRACT_MODE_P (TYPE_MODE (argtype))
4477 ? inc
4478 : integer_one_node),
4479 (code == POSTINCREMENT_EXPR
4480 || code == POSTDECREMENT_EXPR));
4481 goto return_build_unary_op;
4482 }
4483
4484 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4485 val = boolean_increment (code, arg);
4486 else
4487 val = build2 (code, TREE_TYPE (arg), arg, inc);
4488 TREE_SIDE_EFFECTS (val) = 1;
4489 if (TREE_CODE (val) != code)
4490 TREE_NO_WARNING (val) = 1;
4491 ret = val;
4492 goto return_build_unary_op;
4493 }
4494
4495 case ADDR_EXPR:
4496 /* Note that this operation never does default_conversion. */
4497
4498 /* The operand of unary '&' must be an lvalue (which excludes
4499 expressions of type void), or, in C99, the result of a [] or
4500 unary '*' operator. */
4501 if (VOID_TYPE_P (TREE_TYPE (arg))
4502 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4503 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4504 pedwarn (location, 0, "taking address of expression of type %<void%>");
4505
4506 /* Let &* cancel out to simplify resulting code. */
4507 if (INDIRECT_REF_P (arg))
4508 {
4509 /* Don't let this be an lvalue. */
4510 if (lvalue_p (TREE_OPERAND (arg, 0)))
4511 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4512 ret = TREE_OPERAND (arg, 0);
4513 goto return_build_unary_op;
4514 }
4515
4516 /* Anything not already handled and not a true memory reference
4517 or a non-lvalue array is an error. */
4518 if (typecode != FUNCTION_TYPE && !noconvert
4519 && !lvalue_or_else (location, arg, lv_addressof))
4520 return error_mark_node;
4521
4522 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4523 folding later. */
4524 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4525 {
4526 tree inner = build_unary_op (location, code,
4527 C_MAYBE_CONST_EXPR_EXPR (arg),
4528 noconvert);
4529 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4530 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4531 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4532 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4533 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4534 goto return_build_unary_op;
4535 }
4536
4537 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4538 argtype = TREE_TYPE (arg);
4539
4540 /* If the lvalue is const or volatile, merge that into the type
4541 to which the address will point. This is only needed
4542 for function types. */
4543 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4544 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4545 && TREE_CODE (argtype) == FUNCTION_TYPE)
4546 {
4547 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4548 int quals = orig_quals;
4549
4550 if (TREE_READONLY (arg))
4551 quals |= TYPE_QUAL_CONST;
4552 if (TREE_THIS_VOLATILE (arg))
4553 quals |= TYPE_QUAL_VOLATILE;
4554
4555 argtype = c_build_qualified_type (argtype, quals);
4556 }
4557
4558 switch (TREE_CODE (arg))
4559 {
4560 case COMPONENT_REF:
4561 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4562 {
4563 error_at (location, "cannot take address of bit-field %qD",
4564 TREE_OPERAND (arg, 1));
4565 return error_mark_node;
4566 }
4567
4568 /* fall through */
4569
4570 case ARRAY_REF:
4571 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4572 {
4573 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4574 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4575 {
4576 error_at (location, "cannot take address of scalar with "
4577 "reverse storage order");
4578 return error_mark_node;
4579 }
4580
4581 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4582 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4583 warning_at (location, OPT_Wscalar_storage_order,
4584 "address of array with reverse scalar storage "
4585 "order requested");
4586 }
4587
4588 default:
4589 break;
4590 }
4591
4592 if (!c_mark_addressable (arg))
4593 return error_mark_node;
4594
4595 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4596 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4597
4598 argtype = build_pointer_type (argtype);
4599
4600 /* ??? Cope with user tricks that amount to offsetof. Delete this
4601 when we have proper support for integer constant expressions. */
4602 val = get_base_address (arg);
4603 if (val && INDIRECT_REF_P (val)
4604 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4605 {
4606 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4607 goto return_build_unary_op;
4608 }
4609
4610 val = build1 (ADDR_EXPR, argtype, arg);
4611
4612 ret = val;
4613 goto return_build_unary_op;
4614
4615 default:
4616 gcc_unreachable ();
4617 }
4618
4619 if (argtype == 0)
4620 argtype = TREE_TYPE (arg);
4621 if (TREE_CODE (arg) == INTEGER_CST)
4622 ret = (require_constant_value
4623 ? fold_build1_initializer_loc (location, code, argtype, arg)
4624 : fold_build1_loc (location, code, argtype, arg));
4625 else
4626 ret = build1 (code, argtype, arg);
4627 return_build_unary_op:
4628 gcc_assert (ret != error_mark_node);
4629 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4630 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4631 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4632 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4633 ret = note_integer_operands (ret);
4634 if (eptype)
4635 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4636 protected_set_expr_location (ret, location);
4637 return ret;
4638 }
4639
4640 /* Return nonzero if REF is an lvalue valid for this language.
4641 Lvalues can be assigned, unless their type has TYPE_READONLY.
4642 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4643
4644 bool
4645 lvalue_p (const_tree ref)
4646 {
4647 const enum tree_code code = TREE_CODE (ref);
4648
4649 switch (code)
4650 {
4651 case REALPART_EXPR:
4652 case IMAGPART_EXPR:
4653 case COMPONENT_REF:
4654 return lvalue_p (TREE_OPERAND (ref, 0));
4655
4656 case C_MAYBE_CONST_EXPR:
4657 return lvalue_p (TREE_OPERAND (ref, 1));
4658
4659 case COMPOUND_LITERAL_EXPR:
4660 case STRING_CST:
4661 return true;
4662
4663 case INDIRECT_REF:
4664 case ARRAY_REF:
4665 case ARRAY_NOTATION_REF:
4666 case VAR_DECL:
4667 case PARM_DECL:
4668 case RESULT_DECL:
4669 case ERROR_MARK:
4670 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4671 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4672
4673 case BIND_EXPR:
4674 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4675
4676 default:
4677 return false;
4678 }
4679 }
4680 \f
4681 /* Give a warning for storing in something that is read-only in GCC
4682 terms but not const in ISO C terms. */
4683
4684 static void
4685 readonly_warning (tree arg, enum lvalue_use use)
4686 {
4687 switch (use)
4688 {
4689 case lv_assign:
4690 warning (0, "assignment of read-only location %qE", arg);
4691 break;
4692 case lv_increment:
4693 warning (0, "increment of read-only location %qE", arg);
4694 break;
4695 case lv_decrement:
4696 warning (0, "decrement of read-only location %qE", arg);
4697 break;
4698 default:
4699 gcc_unreachable ();
4700 }
4701 return;
4702 }
4703
4704
4705 /* Return nonzero if REF is an lvalue valid for this language;
4706 otherwise, print an error message and return zero. USE says
4707 how the lvalue is being used and so selects the error message.
4708 LOCATION is the location at which any error should be reported. */
4709
4710 static int
4711 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4712 {
4713 int win = lvalue_p (ref);
4714
4715 if (!win)
4716 lvalue_error (loc, use);
4717
4718 return win;
4719 }
4720 \f
4721 /* Mark EXP saying that we need to be able to take the
4722 address of it; it should not be allocated in a register.
4723 Returns true if successful. */
4724
4725 bool
4726 c_mark_addressable (tree exp)
4727 {
4728 tree x = exp;
4729
4730 while (1)
4731 switch (TREE_CODE (x))
4732 {
4733 case COMPONENT_REF:
4734 case ADDR_EXPR:
4735 case ARRAY_REF:
4736 case REALPART_EXPR:
4737 case IMAGPART_EXPR:
4738 x = TREE_OPERAND (x, 0);
4739 break;
4740
4741 case COMPOUND_LITERAL_EXPR:
4742 case CONSTRUCTOR:
4743 TREE_ADDRESSABLE (x) = 1;
4744 return true;
4745
4746 case VAR_DECL:
4747 case CONST_DECL:
4748 case PARM_DECL:
4749 case RESULT_DECL:
4750 if (C_DECL_REGISTER (x)
4751 && DECL_NONLOCAL (x))
4752 {
4753 if (TREE_PUBLIC (x) || is_global_var (x))
4754 {
4755 error
4756 ("global register variable %qD used in nested function", x);
4757 return false;
4758 }
4759 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4760 }
4761 else if (C_DECL_REGISTER (x))
4762 {
4763 if (TREE_PUBLIC (x) || is_global_var (x))
4764 error ("address of global register variable %qD requested", x);
4765 else
4766 error ("address of register variable %qD requested", x);
4767 return false;
4768 }
4769
4770 /* FALLTHRU */
4771 case FUNCTION_DECL:
4772 TREE_ADDRESSABLE (x) = 1;
4773 /* FALLTHRU */
4774 default:
4775 return true;
4776 }
4777 }
4778 \f
4779 /* Convert EXPR to TYPE, warning about conversion problems with
4780 constants. SEMANTIC_TYPE is the type this conversion would use
4781 without excess precision. If SEMANTIC_TYPE is NULL, this function
4782 is equivalent to convert_and_check. This function is a wrapper that
4783 handles conversions that may be different than
4784 the usual ones because of excess precision. */
4785
4786 static tree
4787 ep_convert_and_check (location_t loc, tree type, tree expr,
4788 tree semantic_type)
4789 {
4790 if (TREE_TYPE (expr) == type)
4791 return expr;
4792
4793 if (!semantic_type)
4794 return convert_and_check (loc, type, expr);
4795
4796 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4797 && TREE_TYPE (expr) != semantic_type)
4798 {
4799 /* For integers, we need to check the real conversion, not
4800 the conversion to the excess precision type. */
4801 expr = convert_and_check (loc, semantic_type, expr);
4802 }
4803 /* Result type is the excess precision type, which should be
4804 large enough, so do not check. */
4805 return convert (type, expr);
4806 }
4807
4808 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4809 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4810 if folded to an integer constant then the unselected half may
4811 contain arbitrary operations not normally permitted in constant
4812 expressions. Set the location of the expression to LOC. */
4813
4814 tree
4815 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4816 tree op1, tree op1_original_type, tree op2,
4817 tree op2_original_type)
4818 {
4819 tree type1;
4820 tree type2;
4821 enum tree_code code1;
4822 enum tree_code code2;
4823 tree result_type = NULL;
4824 tree semantic_result_type = NULL;
4825 tree orig_op1 = op1, orig_op2 = op2;
4826 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4827 bool ifexp_int_operands;
4828 tree ret;
4829
4830 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4831 if (op1_int_operands)
4832 op1 = remove_c_maybe_const_expr (op1);
4833 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4834 if (op2_int_operands)
4835 op2 = remove_c_maybe_const_expr (op2);
4836 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4837 if (ifexp_int_operands)
4838 ifexp = remove_c_maybe_const_expr (ifexp);
4839
4840 /* Promote both alternatives. */
4841
4842 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4843 op1 = default_conversion (op1);
4844 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4845 op2 = default_conversion (op2);
4846
4847 if (TREE_CODE (ifexp) == ERROR_MARK
4848 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4849 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4850 return error_mark_node;
4851
4852 type1 = TREE_TYPE (op1);
4853 code1 = TREE_CODE (type1);
4854 type2 = TREE_TYPE (op2);
4855 code2 = TREE_CODE (type2);
4856
4857 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4858 return error_mark_node;
4859
4860 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4861 return error_mark_node;
4862
4863 /* C90 does not permit non-lvalue arrays in conditional expressions.
4864 In C99 they will be pointers by now. */
4865 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4866 {
4867 error_at (colon_loc, "non-lvalue array in conditional expression");
4868 return error_mark_node;
4869 }
4870
4871 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4872 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4873 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4874 || code1 == COMPLEX_TYPE)
4875 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4876 || code2 == COMPLEX_TYPE))
4877 {
4878 semantic_result_type = c_common_type (type1, type2);
4879 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4880 {
4881 op1 = TREE_OPERAND (op1, 0);
4882 type1 = TREE_TYPE (op1);
4883 gcc_assert (TREE_CODE (type1) == code1);
4884 }
4885 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4886 {
4887 op2 = TREE_OPERAND (op2, 0);
4888 type2 = TREE_TYPE (op2);
4889 gcc_assert (TREE_CODE (type2) == code2);
4890 }
4891 }
4892
4893 if (warn_cxx_compat)
4894 {
4895 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4896 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4897
4898 if (TREE_CODE (t1) == ENUMERAL_TYPE
4899 && TREE_CODE (t2) == ENUMERAL_TYPE
4900 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4901 warning_at (colon_loc, OPT_Wc___compat,
4902 ("different enum types in conditional is "
4903 "invalid in C++: %qT vs %qT"),
4904 t1, t2);
4905 }
4906
4907 /* Quickly detect the usual case where op1 and op2 have the same type
4908 after promotion. */
4909 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4910 {
4911 if (type1 == type2)
4912 result_type = type1;
4913 else
4914 result_type = TYPE_MAIN_VARIANT (type1);
4915 }
4916 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4917 || code1 == COMPLEX_TYPE)
4918 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4919 || code2 == COMPLEX_TYPE))
4920 {
4921 result_type = c_common_type (type1, type2);
4922 if (result_type == error_mark_node)
4923 return error_mark_node;
4924 do_warn_double_promotion (result_type, type1, type2,
4925 "implicit conversion from %qT to %qT to "
4926 "match other result of conditional",
4927 colon_loc);
4928
4929 /* If -Wsign-compare, warn here if type1 and type2 have
4930 different signedness. We'll promote the signed to unsigned
4931 and later code won't know it used to be different.
4932 Do this check on the original types, so that explicit casts
4933 will be considered, but default promotions won't. */
4934 if (c_inhibit_evaluation_warnings == 0)
4935 {
4936 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4937 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4938
4939 if (unsigned_op1 ^ unsigned_op2)
4940 {
4941 bool ovf;
4942
4943 /* Do not warn if the result type is signed, since the
4944 signed type will only be chosen if it can represent
4945 all the values of the unsigned type. */
4946 if (!TYPE_UNSIGNED (result_type))
4947 /* OK */;
4948 else
4949 {
4950 bool op1_maybe_const = true;
4951 bool op2_maybe_const = true;
4952
4953 /* Do not warn if the signed quantity is an
4954 unsuffixed integer literal (or some static
4955 constant expression involving such literals) and
4956 it is non-negative. This warning requires the
4957 operands to be folded for best results, so do
4958 that folding in this case even without
4959 warn_sign_compare to avoid warning options
4960 possibly affecting code generation. */
4961 c_inhibit_evaluation_warnings
4962 += (ifexp == truthvalue_false_node);
4963 op1 = c_fully_fold (op1, require_constant_value,
4964 &op1_maybe_const);
4965 c_inhibit_evaluation_warnings
4966 -= (ifexp == truthvalue_false_node);
4967
4968 c_inhibit_evaluation_warnings
4969 += (ifexp == truthvalue_true_node);
4970 op2 = c_fully_fold (op2, require_constant_value,
4971 &op2_maybe_const);
4972 c_inhibit_evaluation_warnings
4973 -= (ifexp == truthvalue_true_node);
4974
4975 if (warn_sign_compare)
4976 {
4977 if ((unsigned_op2
4978 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4979 || (unsigned_op1
4980 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4981 /* OK */;
4982 else
4983 warning_at (colon_loc, OPT_Wsign_compare,
4984 ("signed and unsigned type in "
4985 "conditional expression"));
4986 }
4987 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4988 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4989 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4990 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4991 }
4992 }
4993 }
4994 }
4995 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4996 {
4997 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4998 pedwarn (colon_loc, OPT_Wpedantic,
4999 "ISO C forbids conditional expr with only one void side");
5000 result_type = void_type_node;
5001 }
5002 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5003 {
5004 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5005 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5006 addr_space_t as_common;
5007
5008 if (comp_target_types (colon_loc, type1, type2))
5009 result_type = common_pointer_type (type1, type2);
5010 else if (null_pointer_constant_p (orig_op1))
5011 result_type = type2;
5012 else if (null_pointer_constant_p (orig_op2))
5013 result_type = type1;
5014 else if (!addr_space_superset (as1, as2, &as_common))
5015 {
5016 error_at (colon_loc, "pointers to disjoint address spaces "
5017 "used in conditional expression");
5018 return error_mark_node;
5019 }
5020 else if (VOID_TYPE_P (TREE_TYPE (type1))
5021 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5022 {
5023 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5024 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5025 & ~TYPE_QUALS (TREE_TYPE (type1))))
5026 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5027 "pointer to array loses qualifier "
5028 "in conditional expression");
5029
5030 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5031 pedwarn (colon_loc, OPT_Wpedantic,
5032 "ISO C forbids conditional expr between "
5033 "%<void *%> and function pointer");
5034 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5035 TREE_TYPE (type2)));
5036 }
5037 else if (VOID_TYPE_P (TREE_TYPE (type2))
5038 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5039 {
5040 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5041 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5042 & ~TYPE_QUALS (TREE_TYPE (type2))))
5043 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5044 "pointer to array loses qualifier "
5045 "in conditional expression");
5046
5047 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5048 pedwarn (colon_loc, OPT_Wpedantic,
5049 "ISO C forbids conditional expr between "
5050 "%<void *%> and function pointer");
5051 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5052 TREE_TYPE (type1)));
5053 }
5054 /* Objective-C pointer comparisons are a bit more lenient. */
5055 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5056 result_type = objc_common_type (type1, type2);
5057 else
5058 {
5059 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5060
5061 pedwarn (colon_loc, 0,
5062 "pointer type mismatch in conditional expression");
5063 result_type = build_pointer_type
5064 (build_qualified_type (void_type_node, qual));
5065 }
5066 }
5067 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5068 {
5069 if (!null_pointer_constant_p (orig_op2))
5070 pedwarn (colon_loc, 0,
5071 "pointer/integer type mismatch in conditional expression");
5072 else
5073 {
5074 op2 = null_pointer_node;
5075 }
5076 result_type = type1;
5077 }
5078 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5079 {
5080 if (!null_pointer_constant_p (orig_op1))
5081 pedwarn (colon_loc, 0,
5082 "pointer/integer type mismatch in conditional expression");
5083 else
5084 {
5085 op1 = null_pointer_node;
5086 }
5087 result_type = type2;
5088 }
5089
5090 if (!result_type)
5091 {
5092 if (flag_cond_mismatch)
5093 result_type = void_type_node;
5094 else
5095 {
5096 error_at (colon_loc, "type mismatch in conditional expression");
5097 return error_mark_node;
5098 }
5099 }
5100
5101 /* Merge const and volatile flags of the incoming types. */
5102 result_type
5103 = build_type_variant (result_type,
5104 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5105 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5106
5107 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5108 semantic_result_type);
5109 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5110 semantic_result_type);
5111
5112 if (ifexp_bcp && ifexp == truthvalue_true_node)
5113 {
5114 op2_int_operands = true;
5115 op1 = c_fully_fold (op1, require_constant_value, NULL);
5116 }
5117 if (ifexp_bcp && ifexp == truthvalue_false_node)
5118 {
5119 op1_int_operands = true;
5120 op2 = c_fully_fold (op2, require_constant_value, NULL);
5121 }
5122 int_const = int_operands = (ifexp_int_operands
5123 && op1_int_operands
5124 && op2_int_operands);
5125 if (int_operands)
5126 {
5127 int_const = ((ifexp == truthvalue_true_node
5128 && TREE_CODE (orig_op1) == INTEGER_CST
5129 && !TREE_OVERFLOW (orig_op1))
5130 || (ifexp == truthvalue_false_node
5131 && TREE_CODE (orig_op2) == INTEGER_CST
5132 && !TREE_OVERFLOW (orig_op2)));
5133 }
5134
5135 /* Need to convert condition operand into a vector mask. */
5136 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5137 {
5138 tree vectype = TREE_TYPE (ifexp);
5139 tree elem_type = TREE_TYPE (vectype);
5140 tree zero = build_int_cst (elem_type, 0);
5141 tree zero_vec = build_vector_from_val (vectype, zero);
5142 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5143 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5144 }
5145
5146 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5147 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5148 else
5149 {
5150 if (int_operands)
5151 {
5152 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5153 nested inside of the expression. */
5154 op1 = c_fully_fold (op1, false, NULL);
5155 op2 = c_fully_fold (op2, false, NULL);
5156 }
5157 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5158 if (int_operands)
5159 ret = note_integer_operands (ret);
5160 }
5161 if (semantic_result_type)
5162 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5163
5164 protected_set_expr_location (ret, colon_loc);
5165 return ret;
5166 }
5167 \f
5168 /* Return a compound expression that performs two expressions and
5169 returns the value of the second of them.
5170
5171 LOC is the location of the COMPOUND_EXPR. */
5172
5173 tree
5174 build_compound_expr (location_t loc, tree expr1, tree expr2)
5175 {
5176 bool expr1_int_operands, expr2_int_operands;
5177 tree eptype = NULL_TREE;
5178 tree ret;
5179
5180 if (flag_cilkplus
5181 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5182 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5183 {
5184 error_at (loc,
5185 "spawned function call cannot be part of a comma expression");
5186 return error_mark_node;
5187 }
5188 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5189 if (expr1_int_operands)
5190 expr1 = remove_c_maybe_const_expr (expr1);
5191 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5192 if (expr2_int_operands)
5193 expr2 = remove_c_maybe_const_expr (expr2);
5194
5195 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5196 expr1 = TREE_OPERAND (expr1, 0);
5197 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5198 {
5199 eptype = TREE_TYPE (expr2);
5200 expr2 = TREE_OPERAND (expr2, 0);
5201 }
5202
5203 if (!TREE_SIDE_EFFECTS (expr1))
5204 {
5205 /* The left-hand operand of a comma expression is like an expression
5206 statement: with -Wunused, we should warn if it doesn't have
5207 any side-effects, unless it was explicitly cast to (void). */
5208 if (warn_unused_value)
5209 {
5210 if (VOID_TYPE_P (TREE_TYPE (expr1))
5211 && CONVERT_EXPR_P (expr1))
5212 ; /* (void) a, b */
5213 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5214 && TREE_CODE (expr1) == COMPOUND_EXPR
5215 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5216 ; /* (void) a, (void) b, c */
5217 else
5218 warning_at (loc, OPT_Wunused_value,
5219 "left-hand operand of comma expression has no effect");
5220 }
5221 }
5222 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5223 && warn_unused_value)
5224 {
5225 tree r = expr1;
5226 location_t cloc = loc;
5227 while (TREE_CODE (r) == COMPOUND_EXPR)
5228 {
5229 if (EXPR_HAS_LOCATION (r))
5230 cloc = EXPR_LOCATION (r);
5231 r = TREE_OPERAND (r, 1);
5232 }
5233 if (!TREE_SIDE_EFFECTS (r)
5234 && !VOID_TYPE_P (TREE_TYPE (r))
5235 && !CONVERT_EXPR_P (r))
5236 warning_at (cloc, OPT_Wunused_value,
5237 "right-hand operand of comma expression has no effect");
5238 }
5239
5240 /* With -Wunused, we should also warn if the left-hand operand does have
5241 side-effects, but computes a value which is not used. For example, in
5242 `foo() + bar(), baz()' the result of the `+' operator is not used,
5243 so we should issue a warning. */
5244 else if (warn_unused_value)
5245 warn_if_unused_value (expr1, loc);
5246
5247 if (expr2 == error_mark_node)
5248 return error_mark_node;
5249
5250 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5251
5252 if (flag_isoc99
5253 && expr1_int_operands
5254 && expr2_int_operands)
5255 ret = note_integer_operands (ret);
5256
5257 if (eptype)
5258 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5259
5260 protected_set_expr_location (ret, loc);
5261 return ret;
5262 }
5263
5264 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5265 which we are casting. OTYPE is the type of the expression being
5266 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5267 of the cast. -Wcast-qual appeared on the command line. Named
5268 address space qualifiers are not handled here, because they result
5269 in different warnings. */
5270
5271 static void
5272 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5273 {
5274 tree in_type = type;
5275 tree in_otype = otype;
5276 int added = 0;
5277 int discarded = 0;
5278 bool is_const;
5279
5280 /* Check that the qualifiers on IN_TYPE are a superset of the
5281 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5282 nodes is uninteresting and we stop as soon as we hit a
5283 non-POINTER_TYPE node on either type. */
5284 do
5285 {
5286 in_otype = TREE_TYPE (in_otype);
5287 in_type = TREE_TYPE (in_type);
5288
5289 /* GNU C allows cv-qualified function types. 'const' means the
5290 function is very pure, 'volatile' means it can't return. We
5291 need to warn when such qualifiers are added, not when they're
5292 taken away. */
5293 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5294 && TREE_CODE (in_type) == FUNCTION_TYPE)
5295 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5296 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5297 else
5298 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5299 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5300 }
5301 while (TREE_CODE (in_type) == POINTER_TYPE
5302 && TREE_CODE (in_otype) == POINTER_TYPE);
5303
5304 if (added)
5305 warning_at (loc, OPT_Wcast_qual,
5306 "cast adds %q#v qualifier to function type", added);
5307
5308 if (discarded)
5309 /* There are qualifiers present in IN_OTYPE that are not present
5310 in IN_TYPE. */
5311 warning_at (loc, OPT_Wcast_qual,
5312 "cast discards %qv qualifier from pointer target type",
5313 discarded);
5314
5315 if (added || discarded)
5316 return;
5317
5318 /* A cast from **T to const **T is unsafe, because it can cause a
5319 const value to be changed with no additional warning. We only
5320 issue this warning if T is the same on both sides, and we only
5321 issue the warning if there are the same number of pointers on
5322 both sides, as otherwise the cast is clearly unsafe anyhow. A
5323 cast is unsafe when a qualifier is added at one level and const
5324 is not present at all outer levels.
5325
5326 To issue this warning, we check at each level whether the cast
5327 adds new qualifiers not already seen. We don't need to special
5328 case function types, as they won't have the same
5329 TYPE_MAIN_VARIANT. */
5330
5331 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5332 return;
5333 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5334 return;
5335
5336 in_type = type;
5337 in_otype = otype;
5338 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5339 do
5340 {
5341 in_type = TREE_TYPE (in_type);
5342 in_otype = TREE_TYPE (in_otype);
5343 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5344 && !is_const)
5345 {
5346 warning_at (loc, OPT_Wcast_qual,
5347 "to be safe all intermediate pointers in cast from "
5348 "%qT to %qT must be %<const%> qualified",
5349 otype, type);
5350 break;
5351 }
5352 if (is_const)
5353 is_const = TYPE_READONLY (in_type);
5354 }
5355 while (TREE_CODE (in_type) == POINTER_TYPE);
5356 }
5357
5358 /* Build an expression representing a cast to type TYPE of expression EXPR.
5359 LOC is the location of the cast-- typically the open paren of the cast. */
5360
5361 tree
5362 build_c_cast (location_t loc, tree type, tree expr)
5363 {
5364 tree value;
5365
5366 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5367 expr = TREE_OPERAND (expr, 0);
5368
5369 value = expr;
5370
5371 if (type == error_mark_node || expr == error_mark_node)
5372 return error_mark_node;
5373
5374 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5375 only in <protocol> qualifications. But when constructing cast expressions,
5376 the protocols do matter and must be kept around. */
5377 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5378 return build1 (NOP_EXPR, type, expr);
5379
5380 type = TYPE_MAIN_VARIANT (type);
5381
5382 if (TREE_CODE (type) == ARRAY_TYPE)
5383 {
5384 error_at (loc, "cast specifies array type");
5385 return error_mark_node;
5386 }
5387
5388 if (TREE_CODE (type) == FUNCTION_TYPE)
5389 {
5390 error_at (loc, "cast specifies function type");
5391 return error_mark_node;
5392 }
5393
5394 if (!VOID_TYPE_P (type))
5395 {
5396 value = require_complete_type (loc, value);
5397 if (value == error_mark_node)
5398 return error_mark_node;
5399 }
5400
5401 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5402 {
5403 if (RECORD_OR_UNION_TYPE_P (type))
5404 pedwarn (loc, OPT_Wpedantic,
5405 "ISO C forbids casting nonscalar to the same type");
5406
5407 /* Convert to remove any qualifiers from VALUE's type. */
5408 value = convert (type, value);
5409 }
5410 else if (TREE_CODE (type) == UNION_TYPE)
5411 {
5412 tree field;
5413
5414 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5415 if (TREE_TYPE (field) != error_mark_node
5416 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5417 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5418 break;
5419
5420 if (field)
5421 {
5422 tree t;
5423 bool maybe_const = true;
5424
5425 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5426 t = c_fully_fold (value, false, &maybe_const);
5427 t = build_constructor_single (type, field, t);
5428 if (!maybe_const)
5429 t = c_wrap_maybe_const (t, true);
5430 t = digest_init (loc, type, t,
5431 NULL_TREE, false, true, 0);
5432 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5433 return t;
5434 }
5435 error_at (loc, "cast to union type from type not present in union");
5436 return error_mark_node;
5437 }
5438 else
5439 {
5440 tree otype, ovalue;
5441
5442 if (type == void_type_node)
5443 {
5444 tree t = build1 (CONVERT_EXPR, type, value);
5445 SET_EXPR_LOCATION (t, loc);
5446 return t;
5447 }
5448
5449 otype = TREE_TYPE (value);
5450
5451 /* Optionally warn about potentially worrisome casts. */
5452 if (warn_cast_qual
5453 && TREE_CODE (type) == POINTER_TYPE
5454 && TREE_CODE (otype) == POINTER_TYPE)
5455 handle_warn_cast_qual (loc, type, otype);
5456
5457 /* Warn about conversions between pointers to disjoint
5458 address spaces. */
5459 if (TREE_CODE (type) == POINTER_TYPE
5460 && TREE_CODE (otype) == POINTER_TYPE
5461 && !null_pointer_constant_p (value))
5462 {
5463 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5464 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5465 addr_space_t as_common;
5466
5467 if (!addr_space_superset (as_to, as_from, &as_common))
5468 {
5469 if (ADDR_SPACE_GENERIC_P (as_from))
5470 warning_at (loc, 0, "cast to %s address space pointer "
5471 "from disjoint generic address space pointer",
5472 c_addr_space_name (as_to));
5473
5474 else if (ADDR_SPACE_GENERIC_P (as_to))
5475 warning_at (loc, 0, "cast to generic address space pointer "
5476 "from disjoint %s address space pointer",
5477 c_addr_space_name (as_from));
5478
5479 else
5480 warning_at (loc, 0, "cast to %s address space pointer "
5481 "from disjoint %s address space pointer",
5482 c_addr_space_name (as_to),
5483 c_addr_space_name (as_from));
5484 }
5485 }
5486
5487 /* Warn about possible alignment problems. */
5488 if (STRICT_ALIGNMENT
5489 && TREE_CODE (type) == POINTER_TYPE
5490 && TREE_CODE (otype) == POINTER_TYPE
5491 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5492 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5493 /* Don't warn about opaque types, where the actual alignment
5494 restriction is unknown. */
5495 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5496 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5497 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5498 warning_at (loc, OPT_Wcast_align,
5499 "cast increases required alignment of target type");
5500
5501 if (TREE_CODE (type) == INTEGER_TYPE
5502 && TREE_CODE (otype) == POINTER_TYPE
5503 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5504 /* Unlike conversion of integers to pointers, where the
5505 warning is disabled for converting constants because
5506 of cases such as SIG_*, warn about converting constant
5507 pointers to integers. In some cases it may cause unwanted
5508 sign extension, and a warning is appropriate. */
5509 warning_at (loc, OPT_Wpointer_to_int_cast,
5510 "cast from pointer to integer of different size");
5511
5512 if (TREE_CODE (value) == CALL_EXPR
5513 && TREE_CODE (type) != TREE_CODE (otype))
5514 warning_at (loc, OPT_Wbad_function_cast,
5515 "cast from function call of type %qT "
5516 "to non-matching type %qT", otype, type);
5517
5518 if (TREE_CODE (type) == POINTER_TYPE
5519 && TREE_CODE (otype) == INTEGER_TYPE
5520 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5521 /* Don't warn about converting any constant. */
5522 && !TREE_CONSTANT (value))
5523 warning_at (loc,
5524 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5525 "of different size");
5526
5527 if (warn_strict_aliasing <= 2)
5528 strict_aliasing_warning (otype, type, expr);
5529
5530 /* If pedantic, warn for conversions between function and object
5531 pointer types, except for converting a null pointer constant
5532 to function pointer type. */
5533 if (pedantic
5534 && TREE_CODE (type) == POINTER_TYPE
5535 && TREE_CODE (otype) == POINTER_TYPE
5536 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5537 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5538 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5539 "conversion of function pointer to object pointer type");
5540
5541 if (pedantic
5542 && TREE_CODE (type) == POINTER_TYPE
5543 && TREE_CODE (otype) == POINTER_TYPE
5544 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5545 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5546 && !null_pointer_constant_p (value))
5547 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5548 "conversion of object pointer to function pointer type");
5549
5550 ovalue = value;
5551 value = convert (type, value);
5552
5553 /* Ignore any integer overflow caused by the cast. */
5554 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5555 {
5556 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5557 {
5558 if (!TREE_OVERFLOW (value))
5559 {
5560 /* Avoid clobbering a shared constant. */
5561 value = copy_node (value);
5562 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5563 }
5564 }
5565 else if (TREE_OVERFLOW (value))
5566 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5567 value = wide_int_to_tree (TREE_TYPE (value), value);
5568 }
5569 }
5570
5571 /* Don't let a cast be an lvalue. */
5572 if (lvalue_p (value))
5573 value = non_lvalue_loc (loc, value);
5574
5575 /* Don't allow the results of casting to floating-point or complex
5576 types be confused with actual constants, or casts involving
5577 integer and pointer types other than direct integer-to-integer
5578 and integer-to-pointer be confused with integer constant
5579 expressions and null pointer constants. */
5580 if (TREE_CODE (value) == REAL_CST
5581 || TREE_CODE (value) == COMPLEX_CST
5582 || (TREE_CODE (value) == INTEGER_CST
5583 && !((TREE_CODE (expr) == INTEGER_CST
5584 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5585 || TREE_CODE (expr) == REAL_CST
5586 || TREE_CODE (expr) == COMPLEX_CST)))
5587 value = build1 (NOP_EXPR, type, value);
5588
5589 protected_set_expr_location (value, loc);
5590 return value;
5591 }
5592
5593 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5594 location of the open paren of the cast, or the position of the cast
5595 expr. */
5596 tree
5597 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5598 {
5599 tree type;
5600 tree type_expr = NULL_TREE;
5601 bool type_expr_const = true;
5602 tree ret;
5603 int saved_wsp = warn_strict_prototypes;
5604
5605 /* This avoids warnings about unprototyped casts on
5606 integers. E.g. "#define SIG_DFL (void(*)())0". */
5607 if (TREE_CODE (expr) == INTEGER_CST)
5608 warn_strict_prototypes = 0;
5609 type = groktypename (type_name, &type_expr, &type_expr_const);
5610 warn_strict_prototypes = saved_wsp;
5611
5612 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5613 && reject_gcc_builtin (expr))
5614 return error_mark_node;
5615
5616 ret = build_c_cast (loc, type, expr);
5617 if (type_expr)
5618 {
5619 bool inner_expr_const = true;
5620 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5621 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5622 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5623 && inner_expr_const);
5624 SET_EXPR_LOCATION (ret, loc);
5625 }
5626
5627 if (!EXPR_HAS_LOCATION (ret))
5628 protected_set_expr_location (ret, loc);
5629
5630 /* C++ does not permits types to be defined in a cast, but it
5631 allows references to incomplete types. */
5632 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5633 warning_at (loc, OPT_Wc___compat,
5634 "defining a type in a cast is invalid in C++");
5635
5636 return ret;
5637 }
5638 \f
5639 /* Build an assignment expression of lvalue LHS from value RHS.
5640 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5641 may differ from TREE_TYPE (LHS) for an enum bitfield.
5642 MODIFYCODE is the code for a binary operator that we use
5643 to combine the old value of LHS with RHS to get the new value.
5644 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5645 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5646 which may differ from TREE_TYPE (RHS) for an enum value.
5647
5648 LOCATION is the location of the MODIFYCODE operator.
5649 RHS_LOC is the location of the RHS. */
5650
5651 tree
5652 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5653 enum tree_code modifycode,
5654 location_t rhs_loc, tree rhs, tree rhs_origtype)
5655 {
5656 tree result;
5657 tree newrhs;
5658 tree rhseval = NULL_TREE;
5659 tree rhs_semantic_type = NULL_TREE;
5660 tree lhstype = TREE_TYPE (lhs);
5661 tree olhstype = lhstype;
5662 bool npc;
5663 bool is_atomic_op;
5664
5665 /* Types that aren't fully specified cannot be used in assignments. */
5666 lhs = require_complete_type (location, lhs);
5667
5668 /* Avoid duplicate error messages from operands that had errors. */
5669 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5670 return error_mark_node;
5671
5672 /* Ensure an error for assigning a non-lvalue array to an array in
5673 C90. */
5674 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5675 {
5676 error_at (location, "assignment to expression with array type");
5677 return error_mark_node;
5678 }
5679
5680 /* For ObjC properties, defer this check. */
5681 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5682 return error_mark_node;
5683
5684 is_atomic_op = really_atomic_lvalue (lhs);
5685
5686 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5687 {
5688 rhs_semantic_type = TREE_TYPE (rhs);
5689 rhs = TREE_OPERAND (rhs, 0);
5690 }
5691
5692 newrhs = rhs;
5693
5694 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5695 {
5696 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5697 lhs_origtype, modifycode, rhs_loc, rhs,
5698 rhs_origtype);
5699 if (inner == error_mark_node)
5700 return error_mark_node;
5701 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5702 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5703 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5704 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5705 protected_set_expr_location (result, location);
5706 return result;
5707 }
5708
5709 /* If a binary op has been requested, combine the old LHS value with the RHS
5710 producing the value we should actually store into the LHS. */
5711
5712 if (modifycode != NOP_EXPR)
5713 {
5714 lhs = c_fully_fold (lhs, false, NULL);
5715 lhs = stabilize_reference (lhs);
5716
5717 /* Construct the RHS for any non-atomic compound assignemnt. */
5718 if (!is_atomic_op)
5719 {
5720 /* If in LHS op= RHS the RHS has side-effects, ensure they
5721 are preevaluated before the rest of the assignment expression's
5722 side-effects, because RHS could contain e.g. function calls
5723 that modify LHS. */
5724 if (TREE_SIDE_EFFECTS (rhs))
5725 {
5726 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5727 rhseval = newrhs;
5728 }
5729 newrhs = build_binary_op (location,
5730 modifycode, lhs, newrhs, 1);
5731
5732 /* The original type of the right hand side is no longer
5733 meaningful. */
5734 rhs_origtype = NULL_TREE;
5735 }
5736 }
5737
5738 if (c_dialect_objc ())
5739 {
5740 /* Check if we are modifying an Objective-C property reference;
5741 if so, we need to generate setter calls. */
5742 result = objc_maybe_build_modify_expr (lhs, newrhs);
5743 if (result)
5744 goto return_result;
5745
5746 /* Else, do the check that we postponed for Objective-C. */
5747 if (!lvalue_or_else (location, lhs, lv_assign))
5748 return error_mark_node;
5749 }
5750
5751 /* Give an error for storing in something that is 'const'. */
5752
5753 if (TYPE_READONLY (lhstype)
5754 || (RECORD_OR_UNION_TYPE_P (lhstype)
5755 && C_TYPE_FIELDS_READONLY (lhstype)))
5756 {
5757 readonly_error (location, lhs, lv_assign);
5758 return error_mark_node;
5759 }
5760 else if (TREE_READONLY (lhs))
5761 readonly_warning (lhs, lv_assign);
5762
5763 /* If storing into a structure or union member,
5764 it has probably been given type `int'.
5765 Compute the type that would go with
5766 the actual amount of storage the member occupies. */
5767
5768 if (TREE_CODE (lhs) == COMPONENT_REF
5769 && (TREE_CODE (lhstype) == INTEGER_TYPE
5770 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5771 || TREE_CODE (lhstype) == REAL_TYPE
5772 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5773 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5774
5775 /* If storing in a field that is in actuality a short or narrower than one,
5776 we must store in the field in its actual type. */
5777
5778 if (lhstype != TREE_TYPE (lhs))
5779 {
5780 lhs = copy_node (lhs);
5781 TREE_TYPE (lhs) = lhstype;
5782 }
5783
5784 /* Issue -Wc++-compat warnings about an assignment to an enum type
5785 when LHS does not have its original type. This happens for,
5786 e.g., an enum bitfield in a struct. */
5787 if (warn_cxx_compat
5788 && lhs_origtype != NULL_TREE
5789 && lhs_origtype != lhstype
5790 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5791 {
5792 tree checktype = (rhs_origtype != NULL_TREE
5793 ? rhs_origtype
5794 : TREE_TYPE (rhs));
5795 if (checktype != error_mark_node
5796 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5797 || (is_atomic_op && modifycode != NOP_EXPR)))
5798 warning_at (location, OPT_Wc___compat,
5799 "enum conversion in assignment is invalid in C++");
5800 }
5801
5802 /* If the lhs is atomic, remove that qualifier. */
5803 if (is_atomic_op)
5804 {
5805 lhstype = build_qualified_type (lhstype,
5806 (TYPE_QUALS (lhstype)
5807 & ~TYPE_QUAL_ATOMIC));
5808 olhstype = build_qualified_type (olhstype,
5809 (TYPE_QUALS (lhstype)
5810 & ~TYPE_QUAL_ATOMIC));
5811 }
5812
5813 /* Convert new value to destination type. Fold it first, then
5814 restore any excess precision information, for the sake of
5815 conversion warnings. */
5816
5817 if (!(is_atomic_op && modifycode != NOP_EXPR))
5818 {
5819 npc = null_pointer_constant_p (newrhs);
5820 newrhs = c_fully_fold (newrhs, false, NULL);
5821 if (rhs_semantic_type)
5822 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5823 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5824 rhs_origtype, ic_assign, npc,
5825 NULL_TREE, NULL_TREE, 0);
5826 if (TREE_CODE (newrhs) == ERROR_MARK)
5827 return error_mark_node;
5828 }
5829
5830 /* Emit ObjC write barrier, if necessary. */
5831 if (c_dialect_objc () && flag_objc_gc)
5832 {
5833 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5834 if (result)
5835 {
5836 protected_set_expr_location (result, location);
5837 goto return_result;
5838 }
5839 }
5840
5841 /* Scan operands. */
5842
5843 if (is_atomic_op)
5844 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5845 else
5846 {
5847 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5848 TREE_SIDE_EFFECTS (result) = 1;
5849 protected_set_expr_location (result, location);
5850 }
5851
5852 /* If we got the LHS in a different type for storing in,
5853 convert the result back to the nominal type of LHS
5854 so that the value we return always has the same type
5855 as the LHS argument. */
5856
5857 if (olhstype == TREE_TYPE (result))
5858 goto return_result;
5859
5860 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5861 rhs_origtype, ic_assign, false, NULL_TREE,
5862 NULL_TREE, 0);
5863 protected_set_expr_location (result, location);
5864
5865 return_result:
5866 if (rhseval)
5867 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5868 return result;
5869 }
5870 \f
5871 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5872 This is used to implement -fplan9-extensions. */
5873
5874 static bool
5875 find_anonymous_field_with_type (tree struct_type, tree type)
5876 {
5877 tree field;
5878 bool found;
5879
5880 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
5881 found = false;
5882 for (field = TYPE_FIELDS (struct_type);
5883 field != NULL_TREE;
5884 field = TREE_CHAIN (field))
5885 {
5886 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5887 ? c_build_qualified_type (TREE_TYPE (field),
5888 TYPE_QUAL_ATOMIC)
5889 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5890 if (DECL_NAME (field) == NULL
5891 && comptypes (type, fieldtype))
5892 {
5893 if (found)
5894 return false;
5895 found = true;
5896 }
5897 else if (DECL_NAME (field) == NULL
5898 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
5899 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5900 {
5901 if (found)
5902 return false;
5903 found = true;
5904 }
5905 }
5906 return found;
5907 }
5908
5909 /* RHS is an expression whose type is pointer to struct. If there is
5910 an anonymous field in RHS with type TYPE, then return a pointer to
5911 that field in RHS. This is used with -fplan9-extensions. This
5912 returns NULL if no conversion could be found. */
5913
5914 static tree
5915 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5916 {
5917 tree rhs_struct_type, lhs_main_type;
5918 tree field, found_field;
5919 bool found_sub_field;
5920 tree ret;
5921
5922 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5923 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5924 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
5925
5926 gcc_assert (POINTER_TYPE_P (type));
5927 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5928 ? c_build_qualified_type (TREE_TYPE (type),
5929 TYPE_QUAL_ATOMIC)
5930 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5931
5932 found_field = NULL_TREE;
5933 found_sub_field = false;
5934 for (field = TYPE_FIELDS (rhs_struct_type);
5935 field != NULL_TREE;
5936 field = TREE_CHAIN (field))
5937 {
5938 if (DECL_NAME (field) != NULL_TREE
5939 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
5940 continue;
5941 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5942 ? c_build_qualified_type (TREE_TYPE (field),
5943 TYPE_QUAL_ATOMIC)
5944 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5945 if (comptypes (lhs_main_type, fieldtype))
5946 {
5947 if (found_field != NULL_TREE)
5948 return NULL_TREE;
5949 found_field = field;
5950 }
5951 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5952 lhs_main_type))
5953 {
5954 if (found_field != NULL_TREE)
5955 return NULL_TREE;
5956 found_field = field;
5957 found_sub_field = true;
5958 }
5959 }
5960
5961 if (found_field == NULL_TREE)
5962 return NULL_TREE;
5963
5964 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5965 build_fold_indirect_ref (rhs), found_field,
5966 NULL_TREE);
5967 ret = build_fold_addr_expr_loc (location, ret);
5968
5969 if (found_sub_field)
5970 {
5971 ret = convert_to_anonymous_field (location, type, ret);
5972 gcc_assert (ret != NULL_TREE);
5973 }
5974
5975 return ret;
5976 }
5977
5978 /* Issue an error message for a bad initializer component.
5979 GMSGID identifies the message.
5980 The component name is taken from the spelling stack. */
5981
5982 static void
5983 error_init (location_t loc, const char *gmsgid)
5984 {
5985 char *ofwhat;
5986
5987 /* The gmsgid may be a format string with %< and %>. */
5988 error_at (loc, gmsgid);
5989 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5990 if (*ofwhat)
5991 inform (loc, "(near initialization for %qs)", ofwhat);
5992 }
5993
5994 /* Issue a pedantic warning for a bad initializer component. OPT is
5995 the option OPT_* (from options.h) controlling this warning or 0 if
5996 it is unconditionally given. GMSGID identifies the message. The
5997 component name is taken from the spelling stack. */
5998
5999 static void
6000 pedwarn_init (location_t loc, int opt, const char *gmsgid)
6001 {
6002 char *ofwhat;
6003 bool warned;
6004
6005 /* Use the location where a macro was expanded rather than where
6006 it was defined to make sure macros defined in system headers
6007 but used incorrectly elsewhere are diagnosed. */
6008 source_location exploc = expansion_point_location_if_in_system_header (loc);
6009
6010 /* The gmsgid may be a format string with %< and %>. */
6011 warned = pedwarn (exploc, opt, gmsgid);
6012 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6013 if (*ofwhat && warned)
6014 inform (exploc, "(near initialization for %qs)", ofwhat);
6015 }
6016
6017 /* Issue a warning for a bad initializer component.
6018
6019 OPT is the OPT_W* value corresponding to the warning option that
6020 controls this warning. GMSGID identifies the message. The
6021 component name is taken from the spelling stack. */
6022
6023 static void
6024 warning_init (location_t loc, int opt, const char *gmsgid)
6025 {
6026 char *ofwhat;
6027 bool warned;
6028
6029 /* Use the location where a macro was expanded rather than where
6030 it was defined to make sure macros defined in system headers
6031 but used incorrectly elsewhere are diagnosed. */
6032 source_location exploc = expansion_point_location_if_in_system_header (loc);
6033
6034 /* The gmsgid may be a format string with %< and %>. */
6035 warned = warning_at (exploc, opt, gmsgid);
6036 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6037 if (*ofwhat && warned)
6038 inform (exploc, "(near initialization for %qs)", ofwhat);
6039 }
6040 \f
6041 /* If TYPE is an array type and EXPR is a parenthesized string
6042 constant, warn if pedantic that EXPR is being used to initialize an
6043 object of type TYPE. */
6044
6045 void
6046 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6047 {
6048 if (pedantic
6049 && TREE_CODE (type) == ARRAY_TYPE
6050 && TREE_CODE (expr.value) == STRING_CST
6051 && expr.original_code != STRING_CST)
6052 pedwarn_init (loc, OPT_Wpedantic,
6053 "array initialized from parenthesized string constant");
6054 }
6055
6056 /* Convert value RHS to type TYPE as preparation for an assignment to
6057 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6058 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6059 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6060 constant before any folding.
6061 The real work of conversion is done by `convert'.
6062 The purpose of this function is to generate error messages
6063 for assignments that are not allowed in C.
6064 ERRTYPE says whether it is argument passing, assignment,
6065 initialization or return.
6066
6067 In the following example, '~' denotes where EXPR_LOC and '^' where
6068 LOCATION point to:
6069
6070 f (var); [ic_argpass]
6071 ^ ~~~
6072 x = var; [ic_assign]
6073 ^ ~~~;
6074 int x = var; [ic_init]
6075 ^^^
6076 return x; [ic_return]
6077 ^
6078
6079 FUNCTION is a tree for the function being called.
6080 PARMNUM is the number of the argument, for printing in error messages. */
6081
6082 static tree
6083 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6084 tree rhs, tree origtype, enum impl_conv errtype,
6085 bool null_pointer_constant, tree fundecl,
6086 tree function, int parmnum)
6087 {
6088 enum tree_code codel = TREE_CODE (type);
6089 tree orig_rhs = rhs;
6090 tree rhstype;
6091 enum tree_code coder;
6092 tree rname = NULL_TREE;
6093 bool objc_ok = false;
6094
6095 /* Use the expansion point location to handle cases such as user's
6096 function returning a wrong-type macro defined in a system header. */
6097 location = expansion_point_location_if_in_system_header (location);
6098
6099 if (errtype == ic_argpass)
6100 {
6101 tree selector;
6102 /* Change pointer to function to the function itself for
6103 diagnostics. */
6104 if (TREE_CODE (function) == ADDR_EXPR
6105 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6106 function = TREE_OPERAND (function, 0);
6107
6108 /* Handle an ObjC selector specially for diagnostics. */
6109 selector = objc_message_selector ();
6110 rname = function;
6111 if (selector && parmnum > 2)
6112 {
6113 rname = selector;
6114 parmnum -= 2;
6115 }
6116 }
6117
6118 /* This macro is used to emit diagnostics to ensure that all format
6119 strings are complete sentences, visible to gettext and checked at
6120 compile time. */
6121 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6122 do { \
6123 switch (errtype) \
6124 { \
6125 case ic_argpass: \
6126 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6127 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6128 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6129 "expected %qT but argument is of type %qT", \
6130 type, rhstype); \
6131 break; \
6132 case ic_assign: \
6133 pedwarn (LOCATION, OPT, AS); \
6134 break; \
6135 case ic_init: \
6136 pedwarn_init (LOCATION, OPT, IN); \
6137 break; \
6138 case ic_return: \
6139 pedwarn (LOCATION, OPT, RE); \
6140 break; \
6141 default: \
6142 gcc_unreachable (); \
6143 } \
6144 } while (0)
6145
6146 /* This macro is used to emit diagnostics to ensure that all format
6147 strings are complete sentences, visible to gettext and checked at
6148 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6149 extra parameter to enumerate qualifiers. */
6150 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6151 do { \
6152 switch (errtype) \
6153 { \
6154 case ic_argpass: \
6155 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6156 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6157 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6158 "expected %qT but argument is of type %qT", \
6159 type, rhstype); \
6160 break; \
6161 case ic_assign: \
6162 pedwarn (LOCATION, OPT, AS, QUALS); \
6163 break; \
6164 case ic_init: \
6165 pedwarn (LOCATION, OPT, IN, QUALS); \
6166 break; \
6167 case ic_return: \
6168 pedwarn (LOCATION, OPT, RE, QUALS); \
6169 break; \
6170 default: \
6171 gcc_unreachable (); \
6172 } \
6173 } while (0)
6174
6175 /* This macro is used to emit diagnostics to ensure that all format
6176 strings are complete sentences, visible to gettext and checked at
6177 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6178 warning_at instead of pedwarn. */
6179 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6180 do { \
6181 switch (errtype) \
6182 { \
6183 case ic_argpass: \
6184 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6185 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6186 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6187 "expected %qT but argument is of type %qT", \
6188 type, rhstype); \
6189 break; \
6190 case ic_assign: \
6191 warning_at (LOCATION, OPT, AS, QUALS); \
6192 break; \
6193 case ic_init: \
6194 warning_at (LOCATION, OPT, IN, QUALS); \
6195 break; \
6196 case ic_return: \
6197 warning_at (LOCATION, OPT, RE, QUALS); \
6198 break; \
6199 default: \
6200 gcc_unreachable (); \
6201 } \
6202 } while (0)
6203
6204 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6205 rhs = TREE_OPERAND (rhs, 0);
6206
6207 rhstype = TREE_TYPE (rhs);
6208 coder = TREE_CODE (rhstype);
6209
6210 if (coder == ERROR_MARK)
6211 return error_mark_node;
6212
6213 if (c_dialect_objc ())
6214 {
6215 int parmno;
6216
6217 switch (errtype)
6218 {
6219 case ic_return:
6220 parmno = 0;
6221 break;
6222
6223 case ic_assign:
6224 parmno = -1;
6225 break;
6226
6227 case ic_init:
6228 parmno = -2;
6229 break;
6230
6231 default:
6232 parmno = parmnum;
6233 break;
6234 }
6235
6236 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6237 }
6238
6239 if (warn_cxx_compat)
6240 {
6241 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6242 if (checktype != error_mark_node
6243 && TREE_CODE (type) == ENUMERAL_TYPE
6244 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6245 {
6246 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
6247 G_("enum conversion when passing argument "
6248 "%d of %qE is invalid in C++"),
6249 G_("enum conversion in assignment is "
6250 "invalid in C++"),
6251 G_("enum conversion in initialization is "
6252 "invalid in C++"),
6253 G_("enum conversion in return is "
6254 "invalid in C++"));
6255 }
6256 }
6257
6258 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6259 return rhs;
6260
6261 if (coder == VOID_TYPE)
6262 {
6263 /* Except for passing an argument to an unprototyped function,
6264 this is a constraint violation. When passing an argument to
6265 an unprototyped function, it is compile-time undefined;
6266 making it a constraint in that case was rejected in
6267 DR#252. */
6268 error_at (location, "void value not ignored as it ought to be");
6269 return error_mark_node;
6270 }
6271 rhs = require_complete_type (location, rhs);
6272 if (rhs == error_mark_node)
6273 return error_mark_node;
6274
6275 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6276 return error_mark_node;
6277
6278 /* A non-reference type can convert to a reference. This handles
6279 va_start, va_copy and possibly port built-ins. */
6280 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6281 {
6282 if (!lvalue_p (rhs))
6283 {
6284 error_at (location, "cannot pass rvalue to reference parameter");
6285 return error_mark_node;
6286 }
6287 if (!c_mark_addressable (rhs))
6288 return error_mark_node;
6289 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6290 SET_EXPR_LOCATION (rhs, location);
6291
6292 rhs = convert_for_assignment (location, expr_loc,
6293 build_pointer_type (TREE_TYPE (type)),
6294 rhs, origtype, errtype,
6295 null_pointer_constant, fundecl, function,
6296 parmnum);
6297 if (rhs == error_mark_node)
6298 return error_mark_node;
6299
6300 rhs = build1 (NOP_EXPR, type, rhs);
6301 SET_EXPR_LOCATION (rhs, location);
6302 return rhs;
6303 }
6304 /* Some types can interconvert without explicit casts. */
6305 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6306 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6307 return convert (type, rhs);
6308 /* Arithmetic types all interconvert, and enum is treated like int. */
6309 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6310 || codel == FIXED_POINT_TYPE
6311 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6312 || codel == BOOLEAN_TYPE)
6313 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6314 || coder == FIXED_POINT_TYPE
6315 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6316 || coder == BOOLEAN_TYPE))
6317 {
6318 tree ret;
6319 bool save = in_late_binary_op;
6320 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6321 || (coder == REAL_TYPE
6322 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6323 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
6324 in_late_binary_op = true;
6325 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6326 ? expr_loc : location, type, orig_rhs);
6327 in_late_binary_op = save;
6328 return ret;
6329 }
6330
6331 /* Aggregates in different TUs might need conversion. */
6332 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6333 && codel == coder
6334 && comptypes (type, rhstype))
6335 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6336 ? expr_loc : location, type, rhs);
6337
6338 /* Conversion to a transparent union or record from its member types.
6339 This applies only to function arguments. */
6340 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6341 && TYPE_TRANSPARENT_AGGR (type))
6342 && errtype == ic_argpass)
6343 {
6344 tree memb, marginal_memb = NULL_TREE;
6345
6346 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6347 {
6348 tree memb_type = TREE_TYPE (memb);
6349
6350 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6351 TYPE_MAIN_VARIANT (rhstype)))
6352 break;
6353
6354 if (TREE_CODE (memb_type) != POINTER_TYPE)
6355 continue;
6356
6357 if (coder == POINTER_TYPE)
6358 {
6359 tree ttl = TREE_TYPE (memb_type);
6360 tree ttr = TREE_TYPE (rhstype);
6361
6362 /* Any non-function converts to a [const][volatile] void *
6363 and vice versa; otherwise, targets must be the same.
6364 Meanwhile, the lhs target must have all the qualifiers of
6365 the rhs. */
6366 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6367 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6368 || comp_target_types (location, memb_type, rhstype))
6369 {
6370 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6371 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6372 /* If this type won't generate any warnings, use it. */
6373 if (lquals == rquals
6374 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6375 && TREE_CODE (ttl) == FUNCTION_TYPE)
6376 ? ((lquals | rquals) == rquals)
6377 : ((lquals | rquals) == lquals)))
6378 break;
6379
6380 /* Keep looking for a better type, but remember this one. */
6381 if (!marginal_memb)
6382 marginal_memb = memb;
6383 }
6384 }
6385
6386 /* Can convert integer zero to any pointer type. */
6387 if (null_pointer_constant)
6388 {
6389 rhs = null_pointer_node;
6390 break;
6391 }
6392 }
6393
6394 if (memb || marginal_memb)
6395 {
6396 if (!memb)
6397 {
6398 /* We have only a marginally acceptable member type;
6399 it needs a warning. */
6400 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6401 tree ttr = TREE_TYPE (rhstype);
6402
6403 /* Const and volatile mean something different for function
6404 types, so the usual warnings are not appropriate. */
6405 if (TREE_CODE (ttr) == FUNCTION_TYPE
6406 && TREE_CODE (ttl) == FUNCTION_TYPE)
6407 {
6408 /* Because const and volatile on functions are
6409 restrictions that say the function will not do
6410 certain things, it is okay to use a const or volatile
6411 function where an ordinary one is wanted, but not
6412 vice-versa. */
6413 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6414 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6415 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6416 OPT_Wdiscarded_qualifiers,
6417 G_("passing argument %d of %qE "
6418 "makes %q#v qualified function "
6419 "pointer from unqualified"),
6420 G_("assignment makes %q#v qualified "
6421 "function pointer from "
6422 "unqualified"),
6423 G_("initialization makes %q#v qualified "
6424 "function pointer from "
6425 "unqualified"),
6426 G_("return makes %q#v qualified function "
6427 "pointer from unqualified"),
6428 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6429 }
6430 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6431 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6432 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6433 OPT_Wdiscarded_qualifiers,
6434 G_("passing argument %d of %qE discards "
6435 "%qv qualifier from pointer target type"),
6436 G_("assignment discards %qv qualifier "
6437 "from pointer target type"),
6438 G_("initialization discards %qv qualifier "
6439 "from pointer target type"),
6440 G_("return discards %qv qualifier from "
6441 "pointer target type"),
6442 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6443
6444 memb = marginal_memb;
6445 }
6446
6447 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6448 pedwarn (location, OPT_Wpedantic,
6449 "ISO C prohibits argument conversion to union type");
6450
6451 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6452 return build_constructor_single (type, memb, rhs);
6453 }
6454 }
6455
6456 /* Conversions among pointers */
6457 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6458 && (coder == codel))
6459 {
6460 tree ttl = TREE_TYPE (type);
6461 tree ttr = TREE_TYPE (rhstype);
6462 tree mvl = ttl;
6463 tree mvr = ttr;
6464 bool is_opaque_pointer;
6465 int target_cmp = 0; /* Cache comp_target_types () result. */
6466 addr_space_t asl;
6467 addr_space_t asr;
6468
6469 if (TREE_CODE (mvl) != ARRAY_TYPE)
6470 mvl = (TYPE_ATOMIC (mvl)
6471 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6472 TYPE_QUAL_ATOMIC)
6473 : TYPE_MAIN_VARIANT (mvl));
6474 if (TREE_CODE (mvr) != ARRAY_TYPE)
6475 mvr = (TYPE_ATOMIC (mvr)
6476 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6477 TYPE_QUAL_ATOMIC)
6478 : TYPE_MAIN_VARIANT (mvr));
6479 /* Opaque pointers are treated like void pointers. */
6480 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6481
6482 /* The Plan 9 compiler permits a pointer to a struct to be
6483 automatically converted into a pointer to an anonymous field
6484 within the struct. */
6485 if (flag_plan9_extensions
6486 && RECORD_OR_UNION_TYPE_P (mvl)
6487 && RECORD_OR_UNION_TYPE_P (mvr)
6488 && mvl != mvr)
6489 {
6490 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6491 if (new_rhs != NULL_TREE)
6492 {
6493 rhs = new_rhs;
6494 rhstype = TREE_TYPE (rhs);
6495 coder = TREE_CODE (rhstype);
6496 ttr = TREE_TYPE (rhstype);
6497 mvr = TYPE_MAIN_VARIANT (ttr);
6498 }
6499 }
6500
6501 /* C++ does not allow the implicit conversion void* -> T*. However,
6502 for the purpose of reducing the number of false positives, we
6503 tolerate the special case of
6504
6505 int *p = NULL;
6506
6507 where NULL is typically defined in C to be '(void *) 0'. */
6508 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6509 warning_at (errtype == ic_argpass ? expr_loc : location,
6510 OPT_Wc___compat,
6511 "request for implicit conversion "
6512 "from %qT to %qT not permitted in C++", rhstype, type);
6513
6514 /* See if the pointers point to incompatible address spaces. */
6515 asl = TYPE_ADDR_SPACE (ttl);
6516 asr = TYPE_ADDR_SPACE (ttr);
6517 if (!null_pointer_constant_p (rhs)
6518 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6519 {
6520 switch (errtype)
6521 {
6522 case ic_argpass:
6523 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6524 "non-enclosed address space", parmnum, rname);
6525 break;
6526 case ic_assign:
6527 error_at (location, "assignment from pointer to "
6528 "non-enclosed address space");
6529 break;
6530 case ic_init:
6531 error_at (location, "initialization from pointer to "
6532 "non-enclosed address space");
6533 break;
6534 case ic_return:
6535 error_at (location, "return from pointer to "
6536 "non-enclosed address space");
6537 break;
6538 default:
6539 gcc_unreachable ();
6540 }
6541 return error_mark_node;
6542 }
6543
6544 /* Check if the right-hand side has a format attribute but the
6545 left-hand side doesn't. */
6546 if (warn_suggest_attribute_format
6547 && check_missing_format_attribute (type, rhstype))
6548 {
6549 switch (errtype)
6550 {
6551 case ic_argpass:
6552 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6553 "argument %d of %qE might be "
6554 "a candidate for a format attribute",
6555 parmnum, rname);
6556 break;
6557 case ic_assign:
6558 warning_at (location, OPT_Wsuggest_attribute_format,
6559 "assignment left-hand side might be "
6560 "a candidate for a format attribute");
6561 break;
6562 case ic_init:
6563 warning_at (location, OPT_Wsuggest_attribute_format,
6564 "initialization left-hand side might be "
6565 "a candidate for a format attribute");
6566 break;
6567 case ic_return:
6568 warning_at (location, OPT_Wsuggest_attribute_format,
6569 "return type might be "
6570 "a candidate for a format attribute");
6571 break;
6572 default:
6573 gcc_unreachable ();
6574 }
6575 }
6576
6577 /* Any non-function converts to a [const][volatile] void *
6578 and vice versa; otherwise, targets must be the same.
6579 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6580 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6581 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6582 || (target_cmp = comp_target_types (location, type, rhstype))
6583 || is_opaque_pointer
6584 || ((c_common_unsigned_type (mvl)
6585 == c_common_unsigned_type (mvr))
6586 && (c_common_signed_type (mvl)
6587 == c_common_signed_type (mvr))
6588 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6589 {
6590 /* Warn about loss of qualifers from pointers to arrays with
6591 qualifiers on the element type. */
6592 if (TREE_CODE (ttr) == ARRAY_TYPE)
6593 {
6594 ttr = strip_array_types (ttr);
6595 ttl = strip_array_types (ttl);
6596
6597 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6598 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6599 WARNING_FOR_QUALIFIERS (location, expr_loc,
6600 OPT_Wdiscarded_array_qualifiers,
6601 G_("passing argument %d of %qE discards "
6602 "%qv qualifier from pointer target type"),
6603 G_("assignment discards %qv qualifier "
6604 "from pointer target type"),
6605 G_("initialization discards %qv qualifier "
6606 "from pointer target type"),
6607 G_("return discards %qv qualifier from "
6608 "pointer target type"),
6609 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6610 }
6611 else if (pedantic
6612 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6613 ||
6614 (VOID_TYPE_P (ttr)
6615 && !null_pointer_constant
6616 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6617 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6618 G_("ISO C forbids passing argument %d of "
6619 "%qE between function pointer "
6620 "and %<void *%>"),
6621 G_("ISO C forbids assignment between "
6622 "function pointer and %<void *%>"),
6623 G_("ISO C forbids initialization between "
6624 "function pointer and %<void *%>"),
6625 G_("ISO C forbids return between function "
6626 "pointer and %<void *%>"));
6627 /* Const and volatile mean something different for function types,
6628 so the usual warnings are not appropriate. */
6629 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6630 && TREE_CODE (ttl) != FUNCTION_TYPE)
6631 {
6632 /* Don't warn about loss of qualifier for conversions from
6633 qualified void* to pointers to arrays with corresponding
6634 qualifier on the element type. */
6635 if (!pedantic)
6636 ttl = strip_array_types (ttl);
6637
6638 /* Assignments between atomic and non-atomic objects are OK. */
6639 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6640 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6641 {
6642 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6643 OPT_Wdiscarded_qualifiers,
6644 G_("passing argument %d of %qE discards "
6645 "%qv qualifier from pointer target type"),
6646 G_("assignment discards %qv qualifier "
6647 "from pointer target type"),
6648 G_("initialization discards %qv qualifier "
6649 "from pointer target type"),
6650 G_("return discards %qv qualifier from "
6651 "pointer target type"),
6652 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6653 }
6654 /* If this is not a case of ignoring a mismatch in signedness,
6655 no warning. */
6656 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6657 || target_cmp)
6658 ;
6659 /* If there is a mismatch, do warn. */
6660 else if (warn_pointer_sign)
6661 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6662 G_("pointer targets in passing argument "
6663 "%d of %qE differ in signedness"),
6664 G_("pointer targets in assignment "
6665 "differ in signedness"),
6666 G_("pointer targets in initialization "
6667 "differ in signedness"),
6668 G_("pointer targets in return differ "
6669 "in signedness"));
6670 }
6671 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6672 && TREE_CODE (ttr) == FUNCTION_TYPE)
6673 {
6674 /* Because const and volatile on functions are restrictions
6675 that say the function will not do certain things,
6676 it is okay to use a const or volatile function
6677 where an ordinary one is wanted, but not vice-versa. */
6678 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6679 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6680 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6681 OPT_Wdiscarded_qualifiers,
6682 G_("passing argument %d of %qE makes "
6683 "%q#v qualified function pointer "
6684 "from unqualified"),
6685 G_("assignment makes %q#v qualified function "
6686 "pointer from unqualified"),
6687 G_("initialization makes %q#v qualified "
6688 "function pointer from unqualified"),
6689 G_("return makes %q#v qualified function "
6690 "pointer from unqualified"),
6691 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6692 }
6693 }
6694 else
6695 /* Avoid warning about the volatile ObjC EH puts on decls. */
6696 if (!objc_ok)
6697 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6698 OPT_Wincompatible_pointer_types,
6699 G_("passing argument %d of %qE from "
6700 "incompatible pointer type"),
6701 G_("assignment from incompatible pointer type"),
6702 G_("initialization from incompatible "
6703 "pointer type"),
6704 G_("return from incompatible pointer type"));
6705
6706 return convert (type, rhs);
6707 }
6708 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6709 {
6710 /* ??? This should not be an error when inlining calls to
6711 unprototyped functions. */
6712 error_at (location, "invalid use of non-lvalue array");
6713 return error_mark_node;
6714 }
6715 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6716 {
6717 /* An explicit constant 0 can convert to a pointer,
6718 or one that results from arithmetic, even including
6719 a cast to integer type. */
6720 if (!null_pointer_constant)
6721 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6722 OPT_Wint_conversion,
6723 G_("passing argument %d of %qE makes "
6724 "pointer from integer without a cast"),
6725 G_("assignment makes pointer from integer "
6726 "without a cast"),
6727 G_("initialization makes pointer from "
6728 "integer without a cast"),
6729 G_("return makes pointer from integer "
6730 "without a cast"));
6731
6732 return convert (type, rhs);
6733 }
6734 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6735 {
6736 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6737 OPT_Wint_conversion,
6738 G_("passing argument %d of %qE makes integer "
6739 "from pointer without a cast"),
6740 G_("assignment makes integer from pointer "
6741 "without a cast"),
6742 G_("initialization makes integer from pointer "
6743 "without a cast"),
6744 G_("return makes integer from pointer "
6745 "without a cast"));
6746 return convert (type, rhs);
6747 }
6748 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6749 {
6750 tree ret;
6751 bool save = in_late_binary_op;
6752 in_late_binary_op = true;
6753 ret = convert (type, rhs);
6754 in_late_binary_op = save;
6755 return ret;
6756 }
6757
6758 switch (errtype)
6759 {
6760 case ic_argpass:
6761 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6762 rname);
6763 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6764 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6765 "expected %qT but argument is of type %qT", type, rhstype);
6766 break;
6767 case ic_assign:
6768 error_at (location, "incompatible types when assigning to type %qT from "
6769 "type %qT", type, rhstype);
6770 break;
6771 case ic_init:
6772 error_at (location,
6773 "incompatible types when initializing type %qT using type %qT",
6774 type, rhstype);
6775 break;
6776 case ic_return:
6777 error_at (location,
6778 "incompatible types when returning type %qT but %qT was "
6779 "expected", rhstype, type);
6780 break;
6781 default:
6782 gcc_unreachable ();
6783 }
6784
6785 return error_mark_node;
6786 }
6787 \f
6788 /* If VALUE is a compound expr all of whose expressions are constant, then
6789 return its value. Otherwise, return error_mark_node.
6790
6791 This is for handling COMPOUND_EXPRs as initializer elements
6792 which is allowed with a warning when -pedantic is specified. */
6793
6794 static tree
6795 valid_compound_expr_initializer (tree value, tree endtype)
6796 {
6797 if (TREE_CODE (value) == COMPOUND_EXPR)
6798 {
6799 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6800 == error_mark_node)
6801 return error_mark_node;
6802 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6803 endtype);
6804 }
6805 else if (!initializer_constant_valid_p (value, endtype))
6806 return error_mark_node;
6807 else
6808 return value;
6809 }
6810 \f
6811 /* Perform appropriate conversions on the initial value of a variable,
6812 store it in the declaration DECL,
6813 and print any error messages that are appropriate.
6814 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6815 If the init is invalid, store an ERROR_MARK.
6816
6817 INIT_LOC is the location of the initial value. */
6818
6819 void
6820 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6821 {
6822 tree value, type;
6823 bool npc = false;
6824
6825 /* If variable's type was invalidly declared, just ignore it. */
6826
6827 type = TREE_TYPE (decl);
6828 if (TREE_CODE (type) == ERROR_MARK)
6829 return;
6830
6831 /* Digest the specified initializer into an expression. */
6832
6833 if (init)
6834 npc = null_pointer_constant_p (init);
6835 value = digest_init (init_loc, type, init, origtype, npc,
6836 true, TREE_STATIC (decl));
6837
6838 /* Store the expression if valid; else report error. */
6839
6840 if (!in_system_header_at (input_location)
6841 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6842 warning (OPT_Wtraditional, "traditional C rejects automatic "
6843 "aggregate initialization");
6844
6845 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6846 DECL_INITIAL (decl) = value;
6847
6848 /* ANSI wants warnings about out-of-range constant initializers. */
6849 STRIP_TYPE_NOPS (value);
6850 if (TREE_STATIC (decl))
6851 constant_expression_warning (value);
6852
6853 /* Check if we need to set array size from compound literal size. */
6854 if (TREE_CODE (type) == ARRAY_TYPE
6855 && TYPE_DOMAIN (type) == 0
6856 && value != error_mark_node)
6857 {
6858 tree inside_init = init;
6859
6860 STRIP_TYPE_NOPS (inside_init);
6861 inside_init = fold (inside_init);
6862
6863 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6864 {
6865 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6866
6867 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6868 {
6869 /* For int foo[] = (int [3]){1}; we need to set array size
6870 now since later on array initializer will be just the
6871 brace enclosed list of the compound literal. */
6872 tree etype = strip_array_types (TREE_TYPE (decl));
6873 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6874 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6875 layout_type (type);
6876 layout_decl (cldecl, 0);
6877 TREE_TYPE (decl)
6878 = c_build_qualified_type (type, TYPE_QUALS (etype));
6879 }
6880 }
6881 }
6882 }
6883 \f
6884 /* Methods for storing and printing names for error messages. */
6885
6886 /* Implement a spelling stack that allows components of a name to be pushed
6887 and popped. Each element on the stack is this structure. */
6888
6889 struct spelling
6890 {
6891 int kind;
6892 union
6893 {
6894 unsigned HOST_WIDE_INT i;
6895 const char *s;
6896 } u;
6897 };
6898
6899 #define SPELLING_STRING 1
6900 #define SPELLING_MEMBER 2
6901 #define SPELLING_BOUNDS 3
6902
6903 static struct spelling *spelling; /* Next stack element (unused). */
6904 static struct spelling *spelling_base; /* Spelling stack base. */
6905 static int spelling_size; /* Size of the spelling stack. */
6906
6907 /* Macros to save and restore the spelling stack around push_... functions.
6908 Alternative to SAVE_SPELLING_STACK. */
6909
6910 #define SPELLING_DEPTH() (spelling - spelling_base)
6911 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6912
6913 /* Push an element on the spelling stack with type KIND and assign VALUE
6914 to MEMBER. */
6915
6916 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6917 { \
6918 int depth = SPELLING_DEPTH (); \
6919 \
6920 if (depth >= spelling_size) \
6921 { \
6922 spelling_size += 10; \
6923 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6924 spelling_size); \
6925 RESTORE_SPELLING_DEPTH (depth); \
6926 } \
6927 \
6928 spelling->kind = (KIND); \
6929 spelling->MEMBER = (VALUE); \
6930 spelling++; \
6931 }
6932
6933 /* Push STRING on the stack. Printed literally. */
6934
6935 static void
6936 push_string (const char *string)
6937 {
6938 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6939 }
6940
6941 /* Push a member name on the stack. Printed as '.' STRING. */
6942
6943 static void
6944 push_member_name (tree decl)
6945 {
6946 const char *const string
6947 = (DECL_NAME (decl)
6948 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6949 : _("<anonymous>"));
6950 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6951 }
6952
6953 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6954
6955 static void
6956 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6957 {
6958 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6959 }
6960
6961 /* Compute the maximum size in bytes of the printed spelling. */
6962
6963 static int
6964 spelling_length (void)
6965 {
6966 int size = 0;
6967 struct spelling *p;
6968
6969 for (p = spelling_base; p < spelling; p++)
6970 {
6971 if (p->kind == SPELLING_BOUNDS)
6972 size += 25;
6973 else
6974 size += strlen (p->u.s) + 1;
6975 }
6976
6977 return size;
6978 }
6979
6980 /* Print the spelling to BUFFER and return it. */
6981
6982 static char *
6983 print_spelling (char *buffer)
6984 {
6985 char *d = buffer;
6986 struct spelling *p;
6987
6988 for (p = spelling_base; p < spelling; p++)
6989 if (p->kind == SPELLING_BOUNDS)
6990 {
6991 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6992 d += strlen (d);
6993 }
6994 else
6995 {
6996 const char *s;
6997 if (p->kind == SPELLING_MEMBER)
6998 *d++ = '.';
6999 for (s = p->u.s; (*d = *s++); d++)
7000 ;
7001 }
7002 *d++ = '\0';
7003 return buffer;
7004 }
7005
7006 /* Digest the parser output INIT as an initializer for type TYPE.
7007 Return a C expression of type TYPE to represent the initial value.
7008
7009 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7010
7011 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7012
7013 If INIT is a string constant, STRICT_STRING is true if it is
7014 unparenthesized or we should not warn here for it being parenthesized.
7015 For other types of INIT, STRICT_STRING is not used.
7016
7017 INIT_LOC is the location of the INIT.
7018
7019 REQUIRE_CONSTANT requests an error if non-constant initializers or
7020 elements are seen. */
7021
7022 static tree
7023 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7024 bool null_pointer_constant, bool strict_string,
7025 int require_constant)
7026 {
7027 enum tree_code code = TREE_CODE (type);
7028 tree inside_init = init;
7029 tree semantic_type = NULL_TREE;
7030 bool maybe_const = true;
7031
7032 if (type == error_mark_node
7033 || !init
7034 || error_operand_p (init))
7035 return error_mark_node;
7036
7037 STRIP_TYPE_NOPS (inside_init);
7038
7039 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7040 {
7041 semantic_type = TREE_TYPE (inside_init);
7042 inside_init = TREE_OPERAND (inside_init, 0);
7043 }
7044 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7045 inside_init = decl_constant_value_for_optimization (inside_init);
7046
7047 /* Initialization of an array of chars from a string constant
7048 optionally enclosed in braces. */
7049
7050 if (code == ARRAY_TYPE && inside_init
7051 && TREE_CODE (inside_init) == STRING_CST)
7052 {
7053 tree typ1
7054 = (TYPE_ATOMIC (TREE_TYPE (type))
7055 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7056 TYPE_QUAL_ATOMIC)
7057 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7058 /* Note that an array could be both an array of character type
7059 and an array of wchar_t if wchar_t is signed char or unsigned
7060 char. */
7061 bool char_array = (typ1 == char_type_node
7062 || typ1 == signed_char_type_node
7063 || typ1 == unsigned_char_type_node);
7064 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7065 bool char16_array = !!comptypes (typ1, char16_type_node);
7066 bool char32_array = !!comptypes (typ1, char32_type_node);
7067
7068 if (char_array || wchar_array || char16_array || char32_array)
7069 {
7070 struct c_expr expr;
7071 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7072 expr.value = inside_init;
7073 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7074 expr.original_type = NULL;
7075 maybe_warn_string_init (init_loc, type, expr);
7076
7077 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7078 pedwarn_init (init_loc, OPT_Wpedantic,
7079 "initialization of a flexible array member");
7080
7081 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7082 TYPE_MAIN_VARIANT (type)))
7083 return inside_init;
7084
7085 if (char_array)
7086 {
7087 if (typ2 != char_type_node)
7088 {
7089 error_init (init_loc, "char-array initialized from wide "
7090 "string");
7091 return error_mark_node;
7092 }
7093 }
7094 else
7095 {
7096 if (typ2 == char_type_node)
7097 {
7098 error_init (init_loc, "wide character array initialized "
7099 "from non-wide string");
7100 return error_mark_node;
7101 }
7102 else if (!comptypes(typ1, typ2))
7103 {
7104 error_init (init_loc, "wide character array initialized "
7105 "from incompatible wide string");
7106 return error_mark_node;
7107 }
7108 }
7109
7110 TREE_TYPE (inside_init) = type;
7111 if (TYPE_DOMAIN (type) != 0
7112 && TYPE_SIZE (type) != 0
7113 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7114 {
7115 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7116
7117 /* Subtract the size of a single (possibly wide) character
7118 because it's ok to ignore the terminating null char
7119 that is counted in the length of the constant. */
7120 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7121 (len
7122 - (TYPE_PRECISION (typ1)
7123 / BITS_PER_UNIT))))
7124 pedwarn_init (init_loc, 0,
7125 ("initializer-string for array of chars "
7126 "is too long"));
7127 else if (warn_cxx_compat
7128 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7129 warning_at (init_loc, OPT_Wc___compat,
7130 ("initializer-string for array chars "
7131 "is too long for C++"));
7132 }
7133
7134 return inside_init;
7135 }
7136 else if (INTEGRAL_TYPE_P (typ1))
7137 {
7138 error_init (init_loc, "array of inappropriate type initialized "
7139 "from string constant");
7140 return error_mark_node;
7141 }
7142 }
7143
7144 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7145 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7146 below and handle as a constructor. */
7147 if (code == VECTOR_TYPE
7148 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7149 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7150 && TREE_CONSTANT (inside_init))
7151 {
7152 if (TREE_CODE (inside_init) == VECTOR_CST
7153 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7154 TYPE_MAIN_VARIANT (type)))
7155 return inside_init;
7156
7157 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7158 {
7159 unsigned HOST_WIDE_INT ix;
7160 tree value;
7161 bool constant_p = true;
7162
7163 /* Iterate through elements and check if all constructor
7164 elements are *_CSTs. */
7165 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7166 if (!CONSTANT_CLASS_P (value))
7167 {
7168 constant_p = false;
7169 break;
7170 }
7171
7172 if (constant_p)
7173 return build_vector_from_ctor (type,
7174 CONSTRUCTOR_ELTS (inside_init));
7175 }
7176 }
7177
7178 if (warn_sequence_point)
7179 verify_sequence_points (inside_init);
7180
7181 /* Any type can be initialized
7182 from an expression of the same type, optionally with braces. */
7183
7184 if (inside_init && TREE_TYPE (inside_init) != 0
7185 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7186 TYPE_MAIN_VARIANT (type))
7187 || (code == ARRAY_TYPE
7188 && comptypes (TREE_TYPE (inside_init), type))
7189 || (code == VECTOR_TYPE
7190 && comptypes (TREE_TYPE (inside_init), type))
7191 || (code == POINTER_TYPE
7192 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7193 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7194 TREE_TYPE (type)))))
7195 {
7196 if (code == POINTER_TYPE)
7197 {
7198 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7199 {
7200 if (TREE_CODE (inside_init) == STRING_CST
7201 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7202 inside_init = array_to_pointer_conversion
7203 (init_loc, inside_init);
7204 else
7205 {
7206 error_init (init_loc, "invalid use of non-lvalue array");
7207 return error_mark_node;
7208 }
7209 }
7210 }
7211
7212 if (code == VECTOR_TYPE)
7213 /* Although the types are compatible, we may require a
7214 conversion. */
7215 inside_init = convert (type, inside_init);
7216
7217 if (require_constant
7218 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7219 {
7220 /* As an extension, allow initializing objects with static storage
7221 duration with compound literals (which are then treated just as
7222 the brace enclosed list they contain). Also allow this for
7223 vectors, as we can only assign them with compound literals. */
7224 if (flag_isoc99 && code != VECTOR_TYPE)
7225 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7226 "is not constant");
7227 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7228 inside_init = DECL_INITIAL (decl);
7229 }
7230
7231 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7232 && TREE_CODE (inside_init) != CONSTRUCTOR)
7233 {
7234 error_init (init_loc, "array initialized from non-constant array "
7235 "expression");
7236 return error_mark_node;
7237 }
7238
7239 /* Compound expressions can only occur here if -Wpedantic or
7240 -pedantic-errors is specified. In the later case, we always want
7241 an error. In the former case, we simply want a warning. */
7242 if (require_constant && pedantic
7243 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7244 {
7245 inside_init
7246 = valid_compound_expr_initializer (inside_init,
7247 TREE_TYPE (inside_init));
7248 if (inside_init == error_mark_node)
7249 error_init (init_loc, "initializer element is not constant");
7250 else
7251 pedwarn_init (init_loc, OPT_Wpedantic,
7252 "initializer element is not constant");
7253 if (flag_pedantic_errors)
7254 inside_init = error_mark_node;
7255 }
7256 else if (require_constant
7257 && !initializer_constant_valid_p (inside_init,
7258 TREE_TYPE (inside_init)))
7259 {
7260 error_init (init_loc, "initializer element is not constant");
7261 inside_init = error_mark_node;
7262 }
7263 else if (require_constant && !maybe_const)
7264 pedwarn_init (init_loc, OPT_Wpedantic,
7265 "initializer element is not a constant expression");
7266
7267 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7268 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7269 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7270 type, inside_init, origtype,
7271 ic_init, null_pointer_constant,
7272 NULL_TREE, NULL_TREE, 0);
7273 return inside_init;
7274 }
7275
7276 /* Handle scalar types, including conversions. */
7277
7278 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7279 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7280 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7281 {
7282 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7283 && (TREE_CODE (init) == STRING_CST
7284 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7285 inside_init = init = array_to_pointer_conversion (init_loc, init);
7286 if (semantic_type)
7287 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7288 inside_init);
7289 inside_init
7290 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7291 inside_init, origtype, ic_init,
7292 null_pointer_constant, NULL_TREE, NULL_TREE,
7293 0);
7294
7295 /* Check to see if we have already given an error message. */
7296 if (inside_init == error_mark_node)
7297 ;
7298 else if (require_constant && !TREE_CONSTANT (inside_init))
7299 {
7300 error_init (init_loc, "initializer element is not constant");
7301 inside_init = error_mark_node;
7302 }
7303 else if (require_constant
7304 && !initializer_constant_valid_p (inside_init,
7305 TREE_TYPE (inside_init)))
7306 {
7307 error_init (init_loc, "initializer element is not computable at "
7308 "load time");
7309 inside_init = error_mark_node;
7310 }
7311 else if (require_constant && !maybe_const)
7312 pedwarn_init (init_loc, OPT_Wpedantic,
7313 "initializer element is not a constant expression");
7314
7315 return inside_init;
7316 }
7317
7318 /* Come here only for records and arrays. */
7319
7320 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7321 {
7322 error_init (init_loc, "variable-sized object may not be initialized");
7323 return error_mark_node;
7324 }
7325
7326 error_init (init_loc, "invalid initializer");
7327 return error_mark_node;
7328 }
7329 \f
7330 /* Handle initializers that use braces. */
7331
7332 /* Type of object we are accumulating a constructor for.
7333 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7334 static tree constructor_type;
7335
7336 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7337 left to fill. */
7338 static tree constructor_fields;
7339
7340 /* For an ARRAY_TYPE, this is the specified index
7341 at which to store the next element we get. */
7342 static tree constructor_index;
7343
7344 /* For an ARRAY_TYPE, this is the maximum index. */
7345 static tree constructor_max_index;
7346
7347 /* For a RECORD_TYPE, this is the first field not yet written out. */
7348 static tree constructor_unfilled_fields;
7349
7350 /* For an ARRAY_TYPE, this is the index of the first element
7351 not yet written out. */
7352 static tree constructor_unfilled_index;
7353
7354 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7355 This is so we can generate gaps between fields, when appropriate. */
7356 static tree constructor_bit_index;
7357
7358 /* If we are saving up the elements rather than allocating them,
7359 this is the list of elements so far (in reverse order,
7360 most recent first). */
7361 static vec<constructor_elt, va_gc> *constructor_elements;
7362
7363 /* 1 if constructor should be incrementally stored into a constructor chain,
7364 0 if all the elements should be kept in AVL tree. */
7365 static int constructor_incremental;
7366
7367 /* 1 if so far this constructor's elements are all compile-time constants. */
7368 static int constructor_constant;
7369
7370 /* 1 if so far this constructor's elements are all valid address constants. */
7371 static int constructor_simple;
7372
7373 /* 1 if this constructor has an element that cannot be part of a
7374 constant expression. */
7375 static int constructor_nonconst;
7376
7377 /* 1 if this constructor is erroneous so far. */
7378 static int constructor_erroneous;
7379
7380 /* 1 if this constructor is the universal zero initializer { 0 }. */
7381 static int constructor_zeroinit;
7382
7383 /* Structure for managing pending initializer elements, organized as an
7384 AVL tree. */
7385
7386 struct init_node
7387 {
7388 struct init_node *left, *right;
7389 struct init_node *parent;
7390 int balance;
7391 tree purpose;
7392 tree value;
7393 tree origtype;
7394 };
7395
7396 /* Tree of pending elements at this constructor level.
7397 These are elements encountered out of order
7398 which belong at places we haven't reached yet in actually
7399 writing the output.
7400 Will never hold tree nodes across GC runs. */
7401 static struct init_node *constructor_pending_elts;
7402
7403 /* The SPELLING_DEPTH of this constructor. */
7404 static int constructor_depth;
7405
7406 /* DECL node for which an initializer is being read.
7407 0 means we are reading a constructor expression
7408 such as (struct foo) {...}. */
7409 static tree constructor_decl;
7410
7411 /* Nonzero if this is an initializer for a top-level decl. */
7412 static int constructor_top_level;
7413
7414 /* Nonzero if there were any member designators in this initializer. */
7415 static int constructor_designated;
7416
7417 /* Nesting depth of designator list. */
7418 static int designator_depth;
7419
7420 /* Nonzero if there were diagnosed errors in this designator list. */
7421 static int designator_erroneous;
7422
7423 \f
7424 /* This stack has a level for each implicit or explicit level of
7425 structuring in the initializer, including the outermost one. It
7426 saves the values of most of the variables above. */
7427
7428 struct constructor_range_stack;
7429
7430 struct constructor_stack
7431 {
7432 struct constructor_stack *next;
7433 tree type;
7434 tree fields;
7435 tree index;
7436 tree max_index;
7437 tree unfilled_index;
7438 tree unfilled_fields;
7439 tree bit_index;
7440 vec<constructor_elt, va_gc> *elements;
7441 struct init_node *pending_elts;
7442 int offset;
7443 int depth;
7444 /* If value nonzero, this value should replace the entire
7445 constructor at this level. */
7446 struct c_expr replacement_value;
7447 struct constructor_range_stack *range_stack;
7448 char constant;
7449 char simple;
7450 char nonconst;
7451 char implicit;
7452 char erroneous;
7453 char outer;
7454 char incremental;
7455 char designated;
7456 int designator_depth;
7457 };
7458
7459 static struct constructor_stack *constructor_stack;
7460
7461 /* This stack represents designators from some range designator up to
7462 the last designator in the list. */
7463
7464 struct constructor_range_stack
7465 {
7466 struct constructor_range_stack *next, *prev;
7467 struct constructor_stack *stack;
7468 tree range_start;
7469 tree index;
7470 tree range_end;
7471 tree fields;
7472 };
7473
7474 static struct constructor_range_stack *constructor_range_stack;
7475
7476 /* This stack records separate initializers that are nested.
7477 Nested initializers can't happen in ANSI C, but GNU C allows them
7478 in cases like { ... (struct foo) { ... } ... }. */
7479
7480 struct initializer_stack
7481 {
7482 struct initializer_stack *next;
7483 tree decl;
7484 struct constructor_stack *constructor_stack;
7485 struct constructor_range_stack *constructor_range_stack;
7486 vec<constructor_elt, va_gc> *elements;
7487 struct spelling *spelling;
7488 struct spelling *spelling_base;
7489 int spelling_size;
7490 char top_level;
7491 char require_constant_value;
7492 char require_constant_elements;
7493 };
7494
7495 static struct initializer_stack *initializer_stack;
7496 \f
7497 /* Prepare to parse and output the initializer for variable DECL. */
7498
7499 void
7500 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7501 {
7502 const char *locus;
7503 struct initializer_stack *p = XNEW (struct initializer_stack);
7504
7505 p->decl = constructor_decl;
7506 p->require_constant_value = require_constant_value;
7507 p->require_constant_elements = require_constant_elements;
7508 p->constructor_stack = constructor_stack;
7509 p->constructor_range_stack = constructor_range_stack;
7510 p->elements = constructor_elements;
7511 p->spelling = spelling;
7512 p->spelling_base = spelling_base;
7513 p->spelling_size = spelling_size;
7514 p->top_level = constructor_top_level;
7515 p->next = initializer_stack;
7516 initializer_stack = p;
7517
7518 constructor_decl = decl;
7519 constructor_designated = 0;
7520 constructor_top_level = top_level;
7521
7522 if (decl != 0 && decl != error_mark_node)
7523 {
7524 require_constant_value = TREE_STATIC (decl);
7525 require_constant_elements
7526 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7527 /* For a scalar, you can always use any value to initialize,
7528 even within braces. */
7529 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7530 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7531 }
7532 else
7533 {
7534 require_constant_value = 0;
7535 require_constant_elements = 0;
7536 locus = _("(anonymous)");
7537 }
7538
7539 constructor_stack = 0;
7540 constructor_range_stack = 0;
7541
7542 found_missing_braces = 0;
7543
7544 spelling_base = 0;
7545 spelling_size = 0;
7546 RESTORE_SPELLING_DEPTH (0);
7547
7548 if (locus)
7549 push_string (locus);
7550 }
7551
7552 void
7553 finish_init (void)
7554 {
7555 struct initializer_stack *p = initializer_stack;
7556
7557 /* Free the whole constructor stack of this initializer. */
7558 while (constructor_stack)
7559 {
7560 struct constructor_stack *q = constructor_stack;
7561 constructor_stack = q->next;
7562 free (q);
7563 }
7564
7565 gcc_assert (!constructor_range_stack);
7566
7567 /* Pop back to the data of the outer initializer (if any). */
7568 free (spelling_base);
7569
7570 constructor_decl = p->decl;
7571 require_constant_value = p->require_constant_value;
7572 require_constant_elements = p->require_constant_elements;
7573 constructor_stack = p->constructor_stack;
7574 constructor_range_stack = p->constructor_range_stack;
7575 constructor_elements = p->elements;
7576 spelling = p->spelling;
7577 spelling_base = p->spelling_base;
7578 spelling_size = p->spelling_size;
7579 constructor_top_level = p->top_level;
7580 initializer_stack = p->next;
7581 free (p);
7582 }
7583 \f
7584 /* Call here when we see the initializer is surrounded by braces.
7585 This is instead of a call to push_init_level;
7586 it is matched by a call to pop_init_level.
7587
7588 TYPE is the type to initialize, for a constructor expression.
7589 For an initializer for a decl, TYPE is zero. */
7590
7591 void
7592 really_start_incremental_init (tree type)
7593 {
7594 struct constructor_stack *p = XNEW (struct constructor_stack);
7595
7596 if (type == 0)
7597 type = TREE_TYPE (constructor_decl);
7598
7599 if (VECTOR_TYPE_P (type)
7600 && TYPE_VECTOR_OPAQUE (type))
7601 error ("opaque vector types cannot be initialized");
7602
7603 p->type = constructor_type;
7604 p->fields = constructor_fields;
7605 p->index = constructor_index;
7606 p->max_index = constructor_max_index;
7607 p->unfilled_index = constructor_unfilled_index;
7608 p->unfilled_fields = constructor_unfilled_fields;
7609 p->bit_index = constructor_bit_index;
7610 p->elements = constructor_elements;
7611 p->constant = constructor_constant;
7612 p->simple = constructor_simple;
7613 p->nonconst = constructor_nonconst;
7614 p->erroneous = constructor_erroneous;
7615 p->pending_elts = constructor_pending_elts;
7616 p->depth = constructor_depth;
7617 p->replacement_value.value = 0;
7618 p->replacement_value.original_code = ERROR_MARK;
7619 p->replacement_value.original_type = NULL;
7620 p->implicit = 0;
7621 p->range_stack = 0;
7622 p->outer = 0;
7623 p->incremental = constructor_incremental;
7624 p->designated = constructor_designated;
7625 p->designator_depth = designator_depth;
7626 p->next = 0;
7627 constructor_stack = p;
7628
7629 constructor_constant = 1;
7630 constructor_simple = 1;
7631 constructor_nonconst = 0;
7632 constructor_depth = SPELLING_DEPTH ();
7633 constructor_elements = NULL;
7634 constructor_pending_elts = 0;
7635 constructor_type = type;
7636 constructor_incremental = 1;
7637 constructor_designated = 0;
7638 constructor_zeroinit = 1;
7639 designator_depth = 0;
7640 designator_erroneous = 0;
7641
7642 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7643 {
7644 constructor_fields = TYPE_FIELDS (constructor_type);
7645 /* Skip any nameless bit fields at the beginning. */
7646 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7647 && DECL_NAME (constructor_fields) == 0)
7648 constructor_fields = DECL_CHAIN (constructor_fields);
7649
7650 constructor_unfilled_fields = constructor_fields;
7651 constructor_bit_index = bitsize_zero_node;
7652 }
7653 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7654 {
7655 if (TYPE_DOMAIN (constructor_type))
7656 {
7657 constructor_max_index
7658 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7659
7660 /* Detect non-empty initializations of zero-length arrays. */
7661 if (constructor_max_index == NULL_TREE
7662 && TYPE_SIZE (constructor_type))
7663 constructor_max_index = integer_minus_one_node;
7664
7665 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7666 to initialize VLAs will cause a proper error; avoid tree
7667 checking errors as well by setting a safe value. */
7668 if (constructor_max_index
7669 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7670 constructor_max_index = integer_minus_one_node;
7671
7672 constructor_index
7673 = convert (bitsizetype,
7674 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7675 }
7676 else
7677 {
7678 constructor_index = bitsize_zero_node;
7679 constructor_max_index = NULL_TREE;
7680 }
7681
7682 constructor_unfilled_index = constructor_index;
7683 }
7684 else if (VECTOR_TYPE_P (constructor_type))
7685 {
7686 /* Vectors are like simple fixed-size arrays. */
7687 constructor_max_index =
7688 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7689 constructor_index = bitsize_zero_node;
7690 constructor_unfilled_index = constructor_index;
7691 }
7692 else
7693 {
7694 /* Handle the case of int x = {5}; */
7695 constructor_fields = constructor_type;
7696 constructor_unfilled_fields = constructor_type;
7697 }
7698 }
7699 \f
7700 /* Called when we see an open brace for a nested initializer. Finish
7701 off any pending levels with implicit braces. */
7702 void
7703 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7704 {
7705 while (constructor_stack->implicit)
7706 {
7707 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7708 && constructor_fields == 0)
7709 process_init_element (input_location,
7710 pop_init_level (loc, 1, braced_init_obstack),
7711 true, braced_init_obstack);
7712 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7713 && constructor_max_index
7714 && tree_int_cst_lt (constructor_max_index,
7715 constructor_index))
7716 process_init_element (input_location,
7717 pop_init_level (loc, 1, braced_init_obstack),
7718 true, braced_init_obstack);
7719 else
7720 break;
7721 }
7722 }
7723
7724 /* Push down into a subobject, for initialization.
7725 If this is for an explicit set of braces, IMPLICIT is 0.
7726 If it is because the next element belongs at a lower level,
7727 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7728
7729 void
7730 push_init_level (location_t loc, int implicit,
7731 struct obstack *braced_init_obstack)
7732 {
7733 struct constructor_stack *p;
7734 tree value = NULL_TREE;
7735
7736 /* Unless this is an explicit brace, we need to preserve previous
7737 content if any. */
7738 if (implicit)
7739 {
7740 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
7741 value = find_init_member (constructor_fields, braced_init_obstack);
7742 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7743 value = find_init_member (constructor_index, braced_init_obstack);
7744 }
7745
7746 p = XNEW (struct constructor_stack);
7747 p->type = constructor_type;
7748 p->fields = constructor_fields;
7749 p->index = constructor_index;
7750 p->max_index = constructor_max_index;
7751 p->unfilled_index = constructor_unfilled_index;
7752 p->unfilled_fields = constructor_unfilled_fields;
7753 p->bit_index = constructor_bit_index;
7754 p->elements = constructor_elements;
7755 p->constant = constructor_constant;
7756 p->simple = constructor_simple;
7757 p->nonconst = constructor_nonconst;
7758 p->erroneous = constructor_erroneous;
7759 p->pending_elts = constructor_pending_elts;
7760 p->depth = constructor_depth;
7761 p->replacement_value.value = 0;
7762 p->replacement_value.original_code = ERROR_MARK;
7763 p->replacement_value.original_type = NULL;
7764 p->implicit = implicit;
7765 p->outer = 0;
7766 p->incremental = constructor_incremental;
7767 p->designated = constructor_designated;
7768 p->designator_depth = designator_depth;
7769 p->next = constructor_stack;
7770 p->range_stack = 0;
7771 constructor_stack = p;
7772
7773 constructor_constant = 1;
7774 constructor_simple = 1;
7775 constructor_nonconst = 0;
7776 constructor_depth = SPELLING_DEPTH ();
7777 constructor_elements = NULL;
7778 constructor_incremental = 1;
7779 constructor_designated = 0;
7780 constructor_pending_elts = 0;
7781 if (!implicit)
7782 {
7783 p->range_stack = constructor_range_stack;
7784 constructor_range_stack = 0;
7785 designator_depth = 0;
7786 designator_erroneous = 0;
7787 }
7788
7789 /* Don't die if an entire brace-pair level is superfluous
7790 in the containing level. */
7791 if (constructor_type == 0)
7792 ;
7793 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
7794 {
7795 /* Don't die if there are extra init elts at the end. */
7796 if (constructor_fields == 0)
7797 constructor_type = 0;
7798 else
7799 {
7800 constructor_type = TREE_TYPE (constructor_fields);
7801 push_member_name (constructor_fields);
7802 constructor_depth++;
7803 }
7804 /* If upper initializer is designated, then mark this as
7805 designated too to prevent bogus warnings. */
7806 constructor_designated = p->designated;
7807 }
7808 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7809 {
7810 constructor_type = TREE_TYPE (constructor_type);
7811 push_array_bounds (tree_to_uhwi (constructor_index));
7812 constructor_depth++;
7813 }
7814
7815 if (constructor_type == 0)
7816 {
7817 error_init (loc, "extra brace group at end of initializer");
7818 constructor_fields = 0;
7819 constructor_unfilled_fields = 0;
7820 return;
7821 }
7822
7823 if (value && TREE_CODE (value) == CONSTRUCTOR)
7824 {
7825 constructor_constant = TREE_CONSTANT (value);
7826 constructor_simple = TREE_STATIC (value);
7827 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7828 constructor_elements = CONSTRUCTOR_ELTS (value);
7829 if (!vec_safe_is_empty (constructor_elements)
7830 && (TREE_CODE (constructor_type) == RECORD_TYPE
7831 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7832 set_nonincremental_init (braced_init_obstack);
7833 }
7834
7835 if (implicit == 1)
7836 found_missing_braces = 1;
7837
7838 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7839 {
7840 constructor_fields = TYPE_FIELDS (constructor_type);
7841 /* Skip any nameless bit fields at the beginning. */
7842 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7843 && DECL_NAME (constructor_fields) == 0)
7844 constructor_fields = DECL_CHAIN (constructor_fields);
7845
7846 constructor_unfilled_fields = constructor_fields;
7847 constructor_bit_index = bitsize_zero_node;
7848 }
7849 else if (VECTOR_TYPE_P (constructor_type))
7850 {
7851 /* Vectors are like simple fixed-size arrays. */
7852 constructor_max_index =
7853 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7854 constructor_index = bitsize_int (0);
7855 constructor_unfilled_index = constructor_index;
7856 }
7857 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7858 {
7859 if (TYPE_DOMAIN (constructor_type))
7860 {
7861 constructor_max_index
7862 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7863
7864 /* Detect non-empty initializations of zero-length arrays. */
7865 if (constructor_max_index == NULL_TREE
7866 && TYPE_SIZE (constructor_type))
7867 constructor_max_index = integer_minus_one_node;
7868
7869 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7870 to initialize VLAs will cause a proper error; avoid tree
7871 checking errors as well by setting a safe value. */
7872 if (constructor_max_index
7873 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7874 constructor_max_index = integer_minus_one_node;
7875
7876 constructor_index
7877 = convert (bitsizetype,
7878 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7879 }
7880 else
7881 constructor_index = bitsize_zero_node;
7882
7883 constructor_unfilled_index = constructor_index;
7884 if (value && TREE_CODE (value) == STRING_CST)
7885 {
7886 /* We need to split the char/wchar array into individual
7887 characters, so that we don't have to special case it
7888 everywhere. */
7889 set_nonincremental_init_from_string (value, braced_init_obstack);
7890 }
7891 }
7892 else
7893 {
7894 if (constructor_type != error_mark_node)
7895 warning_init (input_location, 0, "braces around scalar initializer");
7896 constructor_fields = constructor_type;
7897 constructor_unfilled_fields = constructor_type;
7898 }
7899 }
7900
7901 /* At the end of an implicit or explicit brace level,
7902 finish up that level of constructor. If a single expression
7903 with redundant braces initialized that level, return the
7904 c_expr structure for that expression. Otherwise, the original_code
7905 element is set to ERROR_MARK.
7906 If we were outputting the elements as they are read, return 0 as the value
7907 from inner levels (process_init_element ignores that),
7908 but return error_mark_node as the value from the outermost level
7909 (that's what we want to put in DECL_INITIAL).
7910 Otherwise, return a CONSTRUCTOR expression as the value. */
7911
7912 struct c_expr
7913 pop_init_level (location_t loc, int implicit,
7914 struct obstack *braced_init_obstack)
7915 {
7916 struct constructor_stack *p;
7917 struct c_expr ret;
7918 ret.value = 0;
7919 ret.original_code = ERROR_MARK;
7920 ret.original_type = NULL;
7921
7922 if (implicit == 0)
7923 {
7924 /* When we come to an explicit close brace,
7925 pop any inner levels that didn't have explicit braces. */
7926 while (constructor_stack->implicit)
7927 process_init_element (input_location,
7928 pop_init_level (loc, 1, braced_init_obstack),
7929 true, braced_init_obstack);
7930 gcc_assert (!constructor_range_stack);
7931 }
7932
7933 /* Now output all pending elements. */
7934 constructor_incremental = 1;
7935 output_pending_init_elements (1, braced_init_obstack);
7936
7937 p = constructor_stack;
7938
7939 /* Error for initializing a flexible array member, or a zero-length
7940 array member in an inappropriate context. */
7941 if (constructor_type && constructor_fields
7942 && TREE_CODE (constructor_type) == ARRAY_TYPE
7943 && TYPE_DOMAIN (constructor_type)
7944 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7945 {
7946 /* Silently discard empty initializations. The parser will
7947 already have pedwarned for empty brackets. */
7948 if (integer_zerop (constructor_unfilled_index))
7949 constructor_type = NULL_TREE;
7950 else
7951 {
7952 gcc_assert (!TYPE_SIZE (constructor_type));
7953
7954 if (constructor_depth > 2)
7955 error_init (loc, "initialization of flexible array member in a nested context");
7956 else
7957 pedwarn_init (loc, OPT_Wpedantic,
7958 "initialization of a flexible array member");
7959
7960 /* We have already issued an error message for the existence
7961 of a flexible array member not at the end of the structure.
7962 Discard the initializer so that we do not die later. */
7963 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7964 constructor_type = NULL_TREE;
7965 }
7966 }
7967
7968 switch (vec_safe_length (constructor_elements))
7969 {
7970 case 0:
7971 /* Initialization with { } counts as zeroinit. */
7972 constructor_zeroinit = 1;
7973 break;
7974 case 1:
7975 /* This might be zeroinit as well. */
7976 if (integer_zerop ((*constructor_elements)[0].value))
7977 constructor_zeroinit = 1;
7978 break;
7979 default:
7980 /* If the constructor has more than one element, it can't be { 0 }. */
7981 constructor_zeroinit = 0;
7982 break;
7983 }
7984
7985 /* Warn when some structs are initialized with direct aggregation. */
7986 if (!implicit && found_missing_braces && warn_missing_braces
7987 && !constructor_zeroinit)
7988 warning_init (loc, OPT_Wmissing_braces,
7989 "missing braces around initializer");
7990
7991 /* Warn when some struct elements are implicitly initialized to zero. */
7992 if (warn_missing_field_initializers
7993 && constructor_type
7994 && TREE_CODE (constructor_type) == RECORD_TYPE
7995 && constructor_unfilled_fields)
7996 {
7997 /* Do not warn for flexible array members or zero-length arrays. */
7998 while (constructor_unfilled_fields
7999 && (!DECL_SIZE (constructor_unfilled_fields)
8000 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8001 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8002
8003 if (constructor_unfilled_fields
8004 /* Do not warn if this level of the initializer uses member
8005 designators; it is likely to be deliberate. */
8006 && !constructor_designated
8007 /* Do not warn about initializing with { 0 } or with { }. */
8008 && !constructor_zeroinit)
8009 {
8010 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8011 "missing initializer for field %qD of %qT",
8012 constructor_unfilled_fields,
8013 constructor_type))
8014 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8015 "%qD declared here", constructor_unfilled_fields);
8016 }
8017 }
8018
8019 /* Pad out the end of the structure. */
8020 if (p->replacement_value.value)
8021 /* If this closes a superfluous brace pair,
8022 just pass out the element between them. */
8023 ret = p->replacement_value;
8024 else if (constructor_type == 0)
8025 ;
8026 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8027 && TREE_CODE (constructor_type) != ARRAY_TYPE
8028 && !VECTOR_TYPE_P (constructor_type))
8029 {
8030 /* A nonincremental scalar initializer--just return
8031 the element, after verifying there is just one. */
8032 if (vec_safe_is_empty (constructor_elements))
8033 {
8034 if (!constructor_erroneous)
8035 error_init (loc, "empty scalar initializer");
8036 ret.value = error_mark_node;
8037 }
8038 else if (vec_safe_length (constructor_elements) != 1)
8039 {
8040 error_init (loc, "extra elements in scalar initializer");
8041 ret.value = (*constructor_elements)[0].value;
8042 }
8043 else
8044 ret.value = (*constructor_elements)[0].value;
8045 }
8046 else
8047 {
8048 if (constructor_erroneous)
8049 ret.value = error_mark_node;
8050 else
8051 {
8052 ret.value = build_constructor (constructor_type,
8053 constructor_elements);
8054 if (constructor_constant)
8055 TREE_CONSTANT (ret.value) = 1;
8056 if (constructor_constant && constructor_simple)
8057 TREE_STATIC (ret.value) = 1;
8058 if (constructor_nonconst)
8059 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8060 }
8061 }
8062
8063 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8064 {
8065 if (constructor_nonconst)
8066 ret.original_code = C_MAYBE_CONST_EXPR;
8067 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8068 ret.original_code = ERROR_MARK;
8069 }
8070
8071 constructor_type = p->type;
8072 constructor_fields = p->fields;
8073 constructor_index = p->index;
8074 constructor_max_index = p->max_index;
8075 constructor_unfilled_index = p->unfilled_index;
8076 constructor_unfilled_fields = p->unfilled_fields;
8077 constructor_bit_index = p->bit_index;
8078 constructor_elements = p->elements;
8079 constructor_constant = p->constant;
8080 constructor_simple = p->simple;
8081 constructor_nonconst = p->nonconst;
8082 constructor_erroneous = p->erroneous;
8083 constructor_incremental = p->incremental;
8084 constructor_designated = p->designated;
8085 designator_depth = p->designator_depth;
8086 constructor_pending_elts = p->pending_elts;
8087 constructor_depth = p->depth;
8088 if (!p->implicit)
8089 constructor_range_stack = p->range_stack;
8090 RESTORE_SPELLING_DEPTH (constructor_depth);
8091
8092 constructor_stack = p->next;
8093 free (p);
8094
8095 if (ret.value == 0 && constructor_stack == 0)
8096 ret.value = error_mark_node;
8097 return ret;
8098 }
8099
8100 /* Common handling for both array range and field name designators.
8101 ARRAY argument is nonzero for array ranges. Returns zero for success. */
8102
8103 static int
8104 set_designator (location_t loc, int array,
8105 struct obstack *braced_init_obstack)
8106 {
8107 tree subtype;
8108 enum tree_code subcode;
8109
8110 /* Don't die if an entire brace-pair level is superfluous
8111 in the containing level. */
8112 if (constructor_type == 0)
8113 return 1;
8114
8115 /* If there were errors in this designator list already, bail out
8116 silently. */
8117 if (designator_erroneous)
8118 return 1;
8119
8120 if (!designator_depth)
8121 {
8122 gcc_assert (!constructor_range_stack);
8123
8124 /* Designator list starts at the level of closest explicit
8125 braces. */
8126 while (constructor_stack->implicit)
8127 process_init_element (input_location,
8128 pop_init_level (loc, 1, braced_init_obstack),
8129 true, braced_init_obstack);
8130 constructor_designated = 1;
8131 return 0;
8132 }
8133
8134 switch (TREE_CODE (constructor_type))
8135 {
8136 case RECORD_TYPE:
8137 case UNION_TYPE:
8138 subtype = TREE_TYPE (constructor_fields);
8139 if (subtype != error_mark_node)
8140 subtype = TYPE_MAIN_VARIANT (subtype);
8141 break;
8142 case ARRAY_TYPE:
8143 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8144 break;
8145 default:
8146 gcc_unreachable ();
8147 }
8148
8149 subcode = TREE_CODE (subtype);
8150 if (array && subcode != ARRAY_TYPE)
8151 {
8152 error_init (loc, "array index in non-array initializer");
8153 return 1;
8154 }
8155 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8156 {
8157 error_init (loc, "field name not in record or union initializer");
8158 return 1;
8159 }
8160
8161 constructor_designated = 1;
8162 finish_implicit_inits (loc, braced_init_obstack);
8163 push_init_level (loc, 2, braced_init_obstack);
8164 return 0;
8165 }
8166
8167 /* If there are range designators in designator list, push a new designator
8168 to constructor_range_stack. RANGE_END is end of such stack range or
8169 NULL_TREE if there is no range designator at this level. */
8170
8171 static void
8172 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8173 {
8174 struct constructor_range_stack *p;
8175
8176 p = (struct constructor_range_stack *)
8177 obstack_alloc (braced_init_obstack,
8178 sizeof (struct constructor_range_stack));
8179 p->prev = constructor_range_stack;
8180 p->next = 0;
8181 p->fields = constructor_fields;
8182 p->range_start = constructor_index;
8183 p->index = constructor_index;
8184 p->stack = constructor_stack;
8185 p->range_end = range_end;
8186 if (constructor_range_stack)
8187 constructor_range_stack->next = p;
8188 constructor_range_stack = p;
8189 }
8190
8191 /* Within an array initializer, specify the next index to be initialized.
8192 FIRST is that index. If LAST is nonzero, then initialize a range
8193 of indices, running from FIRST through LAST. */
8194
8195 void
8196 set_init_index (location_t loc, tree first, tree last,
8197 struct obstack *braced_init_obstack)
8198 {
8199 if (set_designator (loc, 1, braced_init_obstack))
8200 return;
8201
8202 designator_erroneous = 1;
8203
8204 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8205 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8206 {
8207 error_init (loc, "array index in initializer not of integer type");
8208 return;
8209 }
8210
8211 if (TREE_CODE (first) != INTEGER_CST)
8212 {
8213 first = c_fully_fold (first, false, NULL);
8214 if (TREE_CODE (first) == INTEGER_CST)
8215 pedwarn_init (loc, OPT_Wpedantic,
8216 "array index in initializer is not "
8217 "an integer constant expression");
8218 }
8219
8220 if (last && TREE_CODE (last) != INTEGER_CST)
8221 {
8222 last = c_fully_fold (last, false, NULL);
8223 if (TREE_CODE (last) == INTEGER_CST)
8224 pedwarn_init (loc, OPT_Wpedantic,
8225 "array index in initializer is not "
8226 "an integer constant expression");
8227 }
8228
8229 if (TREE_CODE (first) != INTEGER_CST)
8230 error_init (loc, "nonconstant array index in initializer");
8231 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
8232 error_init (loc, "nonconstant array index in initializer");
8233 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8234 error_init (loc, "array index in non-array initializer");
8235 else if (tree_int_cst_sgn (first) == -1)
8236 error_init (loc, "array index in initializer exceeds array bounds");
8237 else if (constructor_max_index
8238 && tree_int_cst_lt (constructor_max_index, first))
8239 error_init (loc, "array index in initializer exceeds array bounds");
8240 else
8241 {
8242 constant_expression_warning (first);
8243 if (last)
8244 constant_expression_warning (last);
8245 constructor_index = convert (bitsizetype, first);
8246 if (tree_int_cst_lt (constructor_index, first))
8247 {
8248 constructor_index = copy_node (constructor_index);
8249 TREE_OVERFLOW (constructor_index) = 1;
8250 }
8251
8252 if (last)
8253 {
8254 if (tree_int_cst_equal (first, last))
8255 last = 0;
8256 else if (tree_int_cst_lt (last, first))
8257 {
8258 error_init (loc, "empty index range in initializer");
8259 last = 0;
8260 }
8261 else
8262 {
8263 last = convert (bitsizetype, last);
8264 if (constructor_max_index != 0
8265 && tree_int_cst_lt (constructor_max_index, last))
8266 {
8267 error_init (loc, "array index range in initializer exceeds "
8268 "array bounds");
8269 last = 0;
8270 }
8271 }
8272 }
8273
8274 designator_depth++;
8275 designator_erroneous = 0;
8276 if (constructor_range_stack || last)
8277 push_range_stack (last, braced_init_obstack);
8278 }
8279 }
8280
8281 /* Within a struct initializer, specify the next field to be initialized. */
8282
8283 void
8284 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8285 struct obstack *braced_init_obstack)
8286 {
8287 tree field;
8288
8289 if (set_designator (loc, 0, braced_init_obstack))
8290 return;
8291
8292 designator_erroneous = 1;
8293
8294 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8295 {
8296 error_init (loc, "field name not in record or union initializer");
8297 return;
8298 }
8299
8300 field = lookup_field (constructor_type, fieldname);
8301
8302 if (field == 0)
8303 {
8304 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8305 if (guessed_id)
8306 {
8307 gcc_rich_location rich_loc (fieldname_loc);
8308 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8309 error_at_rich_loc
8310 (&rich_loc,
8311 "%qT has no member named %qE; did you mean %qE?",
8312 constructor_type, fieldname, guessed_id);
8313 }
8314 else
8315 error_at (fieldname_loc, "%qT has no member named %qE",
8316 constructor_type, fieldname);
8317 }
8318 else
8319 do
8320 {
8321 constructor_fields = TREE_VALUE (field);
8322 designator_depth++;
8323 designator_erroneous = 0;
8324 if (constructor_range_stack)
8325 push_range_stack (NULL_TREE, braced_init_obstack);
8326 field = TREE_CHAIN (field);
8327 if (field)
8328 {
8329 if (set_designator (loc, 0, braced_init_obstack))
8330 return;
8331 }
8332 }
8333 while (field != NULL_TREE);
8334 }
8335 \f
8336 /* Add a new initializer to the tree of pending initializers. PURPOSE
8337 identifies the initializer, either array index or field in a structure.
8338 VALUE is the value of that index or field. If ORIGTYPE is not
8339 NULL_TREE, it is the original type of VALUE.
8340
8341 IMPLICIT is true if value comes from pop_init_level (1),
8342 the new initializer has been merged with the existing one
8343 and thus no warnings should be emitted about overriding an
8344 existing initializer. */
8345
8346 static void
8347 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8348 bool implicit, struct obstack *braced_init_obstack)
8349 {
8350 struct init_node *p, **q, *r;
8351
8352 q = &constructor_pending_elts;
8353 p = 0;
8354
8355 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8356 {
8357 while (*q != 0)
8358 {
8359 p = *q;
8360 if (tree_int_cst_lt (purpose, p->purpose))
8361 q = &p->left;
8362 else if (tree_int_cst_lt (p->purpose, purpose))
8363 q = &p->right;
8364 else
8365 {
8366 if (!implicit)
8367 {
8368 if (TREE_SIDE_EFFECTS (p->value))
8369 warning_init (loc, OPT_Woverride_init_side_effects,
8370 "initialized field with side-effects "
8371 "overwritten");
8372 else if (warn_override_init)
8373 warning_init (loc, OPT_Woverride_init,
8374 "initialized field overwritten");
8375 }
8376 p->value = value;
8377 p->origtype = origtype;
8378 return;
8379 }
8380 }
8381 }
8382 else
8383 {
8384 tree bitpos;
8385
8386 bitpos = bit_position (purpose);
8387 while (*q != NULL)
8388 {
8389 p = *q;
8390 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8391 q = &p->left;
8392 else if (p->purpose != purpose)
8393 q = &p->right;
8394 else
8395 {
8396 if (!implicit)
8397 {
8398 if (TREE_SIDE_EFFECTS (p->value))
8399 warning_init (loc, OPT_Woverride_init_side_effects,
8400 "initialized field with side-effects "
8401 "overwritten");
8402 else if (warn_override_init)
8403 warning_init (loc, OPT_Woverride_init,
8404 "initialized field overwritten");
8405 }
8406 p->value = value;
8407 p->origtype = origtype;
8408 return;
8409 }
8410 }
8411 }
8412
8413 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8414 sizeof (struct init_node));
8415 r->purpose = purpose;
8416 r->value = value;
8417 r->origtype = origtype;
8418
8419 *q = r;
8420 r->parent = p;
8421 r->left = 0;
8422 r->right = 0;
8423 r->balance = 0;
8424
8425 while (p)
8426 {
8427 struct init_node *s;
8428
8429 if (r == p->left)
8430 {
8431 if (p->balance == 0)
8432 p->balance = -1;
8433 else if (p->balance < 0)
8434 {
8435 if (r->balance < 0)
8436 {
8437 /* L rotation. */
8438 p->left = r->right;
8439 if (p->left)
8440 p->left->parent = p;
8441 r->right = p;
8442
8443 p->balance = 0;
8444 r->balance = 0;
8445
8446 s = p->parent;
8447 p->parent = r;
8448 r->parent = s;
8449 if (s)
8450 {
8451 if (s->left == p)
8452 s->left = r;
8453 else
8454 s->right = r;
8455 }
8456 else
8457 constructor_pending_elts = r;
8458 }
8459 else
8460 {
8461 /* LR rotation. */
8462 struct init_node *t = r->right;
8463
8464 r->right = t->left;
8465 if (r->right)
8466 r->right->parent = r;
8467 t->left = r;
8468
8469 p->left = t->right;
8470 if (p->left)
8471 p->left->parent = p;
8472 t->right = p;
8473
8474 p->balance = t->balance < 0;
8475 r->balance = -(t->balance > 0);
8476 t->balance = 0;
8477
8478 s = p->parent;
8479 p->parent = t;
8480 r->parent = t;
8481 t->parent = s;
8482 if (s)
8483 {
8484 if (s->left == p)
8485 s->left = t;
8486 else
8487 s->right = t;
8488 }
8489 else
8490 constructor_pending_elts = t;
8491 }
8492 break;
8493 }
8494 else
8495 {
8496 /* p->balance == +1; growth of left side balances the node. */
8497 p->balance = 0;
8498 break;
8499 }
8500 }
8501 else /* r == p->right */
8502 {
8503 if (p->balance == 0)
8504 /* Growth propagation from right side. */
8505 p->balance++;
8506 else if (p->balance > 0)
8507 {
8508 if (r->balance > 0)
8509 {
8510 /* R rotation. */
8511 p->right = r->left;
8512 if (p->right)
8513 p->right->parent = p;
8514 r->left = p;
8515
8516 p->balance = 0;
8517 r->balance = 0;
8518
8519 s = p->parent;
8520 p->parent = r;
8521 r->parent = s;
8522 if (s)
8523 {
8524 if (s->left == p)
8525 s->left = r;
8526 else
8527 s->right = r;
8528 }
8529 else
8530 constructor_pending_elts = r;
8531 }
8532 else /* r->balance == -1 */
8533 {
8534 /* RL rotation */
8535 struct init_node *t = r->left;
8536
8537 r->left = t->right;
8538 if (r->left)
8539 r->left->parent = r;
8540 t->right = r;
8541
8542 p->right = t->left;
8543 if (p->right)
8544 p->right->parent = p;
8545 t->left = p;
8546
8547 r->balance = (t->balance < 0);
8548 p->balance = -(t->balance > 0);
8549 t->balance = 0;
8550
8551 s = p->parent;
8552 p->parent = t;
8553 r->parent = t;
8554 t->parent = s;
8555 if (s)
8556 {
8557 if (s->left == p)
8558 s->left = t;
8559 else
8560 s->right = t;
8561 }
8562 else
8563 constructor_pending_elts = t;
8564 }
8565 break;
8566 }
8567 else
8568 {
8569 /* p->balance == -1; growth of right side balances the node. */
8570 p->balance = 0;
8571 break;
8572 }
8573 }
8574
8575 r = p;
8576 p = p->parent;
8577 }
8578 }
8579
8580 /* Build AVL tree from a sorted chain. */
8581
8582 static void
8583 set_nonincremental_init (struct obstack * braced_init_obstack)
8584 {
8585 unsigned HOST_WIDE_INT ix;
8586 tree index, value;
8587
8588 if (TREE_CODE (constructor_type) != RECORD_TYPE
8589 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8590 return;
8591
8592 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8593 add_pending_init (input_location, index, value, NULL_TREE, true,
8594 braced_init_obstack);
8595 constructor_elements = NULL;
8596 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8597 {
8598 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8599 /* Skip any nameless bit fields at the beginning. */
8600 while (constructor_unfilled_fields != 0
8601 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8602 && DECL_NAME (constructor_unfilled_fields) == 0)
8603 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8604
8605 }
8606 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8607 {
8608 if (TYPE_DOMAIN (constructor_type))
8609 constructor_unfilled_index
8610 = convert (bitsizetype,
8611 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8612 else
8613 constructor_unfilled_index = bitsize_zero_node;
8614 }
8615 constructor_incremental = 0;
8616 }
8617
8618 /* Build AVL tree from a string constant. */
8619
8620 static void
8621 set_nonincremental_init_from_string (tree str,
8622 struct obstack * braced_init_obstack)
8623 {
8624 tree value, purpose, type;
8625 HOST_WIDE_INT val[2];
8626 const char *p, *end;
8627 int byte, wchar_bytes, charwidth, bitpos;
8628
8629 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8630
8631 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8632 charwidth = TYPE_PRECISION (char_type_node);
8633 gcc_assert ((size_t) wchar_bytes * charwidth
8634 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
8635 type = TREE_TYPE (constructor_type);
8636 p = TREE_STRING_POINTER (str);
8637 end = p + TREE_STRING_LENGTH (str);
8638
8639 for (purpose = bitsize_zero_node;
8640 p < end
8641 && !(constructor_max_index
8642 && tree_int_cst_lt (constructor_max_index, purpose));
8643 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8644 {
8645 if (wchar_bytes == 1)
8646 {
8647 val[0] = (unsigned char) *p++;
8648 val[1] = 0;
8649 }
8650 else
8651 {
8652 val[1] = 0;
8653 val[0] = 0;
8654 for (byte = 0; byte < wchar_bytes; byte++)
8655 {
8656 if (BYTES_BIG_ENDIAN)
8657 bitpos = (wchar_bytes - byte - 1) * charwidth;
8658 else
8659 bitpos = byte * charwidth;
8660 val[bitpos / HOST_BITS_PER_WIDE_INT]
8661 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8662 << (bitpos % HOST_BITS_PER_WIDE_INT);
8663 }
8664 }
8665
8666 if (!TYPE_UNSIGNED (type))
8667 {
8668 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8669 if (bitpos < HOST_BITS_PER_WIDE_INT)
8670 {
8671 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
8672 {
8673 val[0] |= HOST_WIDE_INT_M1U << bitpos;
8674 val[1] = -1;
8675 }
8676 }
8677 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8678 {
8679 if (val[0] < 0)
8680 val[1] = -1;
8681 }
8682 else if (val[1] & (HOST_WIDE_INT_1
8683 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8684 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
8685 }
8686
8687 value = wide_int_to_tree (type,
8688 wide_int::from_array (val, 2,
8689 HOST_BITS_PER_WIDE_INT * 2));
8690 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8691 braced_init_obstack);
8692 }
8693
8694 constructor_incremental = 0;
8695 }
8696
8697 /* Return value of FIELD in pending initializer or zero if the field was
8698 not initialized yet. */
8699
8700 static tree
8701 find_init_member (tree field, struct obstack * braced_init_obstack)
8702 {
8703 struct init_node *p;
8704
8705 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8706 {
8707 if (constructor_incremental
8708 && tree_int_cst_lt (field, constructor_unfilled_index))
8709 set_nonincremental_init (braced_init_obstack);
8710
8711 p = constructor_pending_elts;
8712 while (p)
8713 {
8714 if (tree_int_cst_lt (field, p->purpose))
8715 p = p->left;
8716 else if (tree_int_cst_lt (p->purpose, field))
8717 p = p->right;
8718 else
8719 return p->value;
8720 }
8721 }
8722 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8723 {
8724 tree bitpos = bit_position (field);
8725
8726 if (constructor_incremental
8727 && (!constructor_unfilled_fields
8728 || tree_int_cst_lt (bitpos,
8729 bit_position (constructor_unfilled_fields))))
8730 set_nonincremental_init (braced_init_obstack);
8731
8732 p = constructor_pending_elts;
8733 while (p)
8734 {
8735 if (field == p->purpose)
8736 return p->value;
8737 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8738 p = p->left;
8739 else
8740 p = p->right;
8741 }
8742 }
8743 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8744 {
8745 if (!vec_safe_is_empty (constructor_elements)
8746 && (constructor_elements->last ().index == field))
8747 return constructor_elements->last ().value;
8748 }
8749 return 0;
8750 }
8751
8752 /* "Output" the next constructor element.
8753 At top level, really output it to assembler code now.
8754 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8755 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8756 TYPE is the data type that the containing data type wants here.
8757 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8758 If VALUE is a string constant, STRICT_STRING is true if it is
8759 unparenthesized or we should not warn here for it being parenthesized.
8760 For other types of VALUE, STRICT_STRING is not used.
8761
8762 PENDING if non-nil means output pending elements that belong
8763 right after this element. (PENDING is normally 1;
8764 it is 0 while outputting pending elements, to avoid recursion.)
8765
8766 IMPLICIT is true if value comes from pop_init_level (1),
8767 the new initializer has been merged with the existing one
8768 and thus no warnings should be emitted about overriding an
8769 existing initializer. */
8770
8771 static void
8772 output_init_element (location_t loc, tree value, tree origtype,
8773 bool strict_string, tree type, tree field, int pending,
8774 bool implicit, struct obstack * braced_init_obstack)
8775 {
8776 tree semantic_type = NULL_TREE;
8777 bool maybe_const = true;
8778 bool npc;
8779
8780 if (type == error_mark_node || value == error_mark_node)
8781 {
8782 constructor_erroneous = 1;
8783 return;
8784 }
8785 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8786 && (TREE_CODE (value) == STRING_CST
8787 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8788 && !(TREE_CODE (value) == STRING_CST
8789 && TREE_CODE (type) == ARRAY_TYPE
8790 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8791 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8792 TYPE_MAIN_VARIANT (type)))
8793 value = array_to_pointer_conversion (input_location, value);
8794
8795 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8796 && require_constant_value && pending)
8797 {
8798 /* As an extension, allow initializing objects with static storage
8799 duration with compound literals (which are then treated just as
8800 the brace enclosed list they contain). */
8801 if (flag_isoc99)
8802 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8803 "constant");
8804 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8805 value = DECL_INITIAL (decl);
8806 }
8807
8808 npc = null_pointer_constant_p (value);
8809 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8810 {
8811 semantic_type = TREE_TYPE (value);
8812 value = TREE_OPERAND (value, 0);
8813 }
8814 value = c_fully_fold (value, require_constant_value, &maybe_const);
8815
8816 if (value == error_mark_node)
8817 constructor_erroneous = 1;
8818 else if (!TREE_CONSTANT (value))
8819 constructor_constant = 0;
8820 else if (!initializer_constant_valid_p (value,
8821 TREE_TYPE (value),
8822 AGGREGATE_TYPE_P (constructor_type)
8823 && TYPE_REVERSE_STORAGE_ORDER
8824 (constructor_type))
8825 || (RECORD_OR_UNION_TYPE_P (constructor_type)
8826 && DECL_C_BIT_FIELD (field)
8827 && TREE_CODE (value) != INTEGER_CST))
8828 constructor_simple = 0;
8829 if (!maybe_const)
8830 constructor_nonconst = 1;
8831
8832 /* Digest the initializer and issue any errors about incompatible
8833 types before issuing errors about non-constant initializers. */
8834 tree new_value = value;
8835 if (semantic_type)
8836 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8837 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
8838 require_constant_value);
8839 if (new_value == error_mark_node)
8840 {
8841 constructor_erroneous = 1;
8842 return;
8843 }
8844 if (require_constant_value || require_constant_elements)
8845 constant_expression_warning (new_value);
8846
8847 /* Proceed to check the constness of the original initializer. */
8848 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8849 {
8850 if (require_constant_value)
8851 {
8852 error_init (loc, "initializer element is not constant");
8853 value = error_mark_node;
8854 }
8855 else if (require_constant_elements)
8856 pedwarn (loc, OPT_Wpedantic,
8857 "initializer element is not computable at load time");
8858 }
8859 else if (!maybe_const
8860 && (require_constant_value || require_constant_elements))
8861 pedwarn_init (loc, OPT_Wpedantic,
8862 "initializer element is not a constant expression");
8863
8864 /* Issue -Wc++-compat warnings about initializing a bitfield with
8865 enum type. */
8866 if (warn_cxx_compat
8867 && field != NULL_TREE
8868 && TREE_CODE (field) == FIELD_DECL
8869 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8870 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8871 != TYPE_MAIN_VARIANT (type))
8872 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8873 {
8874 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8875 if (checktype != error_mark_node
8876 && (TYPE_MAIN_VARIANT (checktype)
8877 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8878 warning_init (loc, OPT_Wc___compat,
8879 "enum conversion in initialization is invalid in C++");
8880 }
8881
8882 /* If this field is empty (and not at the end of structure),
8883 don't do anything other than checking the initializer. */
8884 if (field
8885 && (TREE_TYPE (field) == error_mark_node
8886 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8887 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8888 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8889 || DECL_CHAIN (field)))))
8890 return;
8891
8892 /* Finally, set VALUE to the initializer value digested above. */
8893 value = new_value;
8894
8895 /* If this element doesn't come next in sequence,
8896 put it on constructor_pending_elts. */
8897 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8898 && (!constructor_incremental
8899 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8900 {
8901 if (constructor_incremental
8902 && tree_int_cst_lt (field, constructor_unfilled_index))
8903 set_nonincremental_init (braced_init_obstack);
8904
8905 add_pending_init (loc, field, value, origtype, implicit,
8906 braced_init_obstack);
8907 return;
8908 }
8909 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8910 && (!constructor_incremental
8911 || field != constructor_unfilled_fields))
8912 {
8913 /* We do this for records but not for unions. In a union,
8914 no matter which field is specified, it can be initialized
8915 right away since it starts at the beginning of the union. */
8916 if (constructor_incremental)
8917 {
8918 if (!constructor_unfilled_fields)
8919 set_nonincremental_init (braced_init_obstack);
8920 else
8921 {
8922 tree bitpos, unfillpos;
8923
8924 bitpos = bit_position (field);
8925 unfillpos = bit_position (constructor_unfilled_fields);
8926
8927 if (tree_int_cst_lt (bitpos, unfillpos))
8928 set_nonincremental_init (braced_init_obstack);
8929 }
8930 }
8931
8932 add_pending_init (loc, field, value, origtype, implicit,
8933 braced_init_obstack);
8934 return;
8935 }
8936 else if (TREE_CODE (constructor_type) == UNION_TYPE
8937 && !vec_safe_is_empty (constructor_elements))
8938 {
8939 if (!implicit)
8940 {
8941 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8942 warning_init (loc, OPT_Woverride_init_side_effects,
8943 "initialized field with side-effects overwritten");
8944 else if (warn_override_init)
8945 warning_init (loc, OPT_Woverride_init,
8946 "initialized field overwritten");
8947 }
8948
8949 /* We can have just one union field set. */
8950 constructor_elements = NULL;
8951 }
8952
8953 /* Otherwise, output this element either to
8954 constructor_elements or to the assembler file. */
8955
8956 constructor_elt celt = {field, value};
8957 vec_safe_push (constructor_elements, celt);
8958
8959 /* Advance the variable that indicates sequential elements output. */
8960 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8961 constructor_unfilled_index
8962 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8963 bitsize_one_node);
8964 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8965 {
8966 constructor_unfilled_fields
8967 = DECL_CHAIN (constructor_unfilled_fields);
8968
8969 /* Skip any nameless bit fields. */
8970 while (constructor_unfilled_fields != 0
8971 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8972 && DECL_NAME (constructor_unfilled_fields) == 0)
8973 constructor_unfilled_fields =
8974 DECL_CHAIN (constructor_unfilled_fields);
8975 }
8976 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8977 constructor_unfilled_fields = 0;
8978
8979 /* Now output any pending elements which have become next. */
8980 if (pending)
8981 output_pending_init_elements (0, braced_init_obstack);
8982 }
8983
8984 /* Output any pending elements which have become next.
8985 As we output elements, constructor_unfilled_{fields,index}
8986 advances, which may cause other elements to become next;
8987 if so, they too are output.
8988
8989 If ALL is 0, we return when there are
8990 no more pending elements to output now.
8991
8992 If ALL is 1, we output space as necessary so that
8993 we can output all the pending elements. */
8994 static void
8995 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8996 {
8997 struct init_node *elt = constructor_pending_elts;
8998 tree next;
8999
9000 retry:
9001
9002 /* Look through the whole pending tree.
9003 If we find an element that should be output now,
9004 output it. Otherwise, set NEXT to the element
9005 that comes first among those still pending. */
9006
9007 next = 0;
9008 while (elt)
9009 {
9010 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9011 {
9012 if (tree_int_cst_equal (elt->purpose,
9013 constructor_unfilled_index))
9014 output_init_element (input_location, elt->value, elt->origtype,
9015 true, TREE_TYPE (constructor_type),
9016 constructor_unfilled_index, 0, false,
9017 braced_init_obstack);
9018 else if (tree_int_cst_lt (constructor_unfilled_index,
9019 elt->purpose))
9020 {
9021 /* Advance to the next smaller node. */
9022 if (elt->left)
9023 elt = elt->left;
9024 else
9025 {
9026 /* We have reached the smallest node bigger than the
9027 current unfilled index. Fill the space first. */
9028 next = elt->purpose;
9029 break;
9030 }
9031 }
9032 else
9033 {
9034 /* Advance to the next bigger node. */
9035 if (elt->right)
9036 elt = elt->right;
9037 else
9038 {
9039 /* We have reached the biggest node in a subtree. Find
9040 the parent of it, which is the next bigger node. */
9041 while (elt->parent && elt->parent->right == elt)
9042 elt = elt->parent;
9043 elt = elt->parent;
9044 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9045 elt->purpose))
9046 {
9047 next = elt->purpose;
9048 break;
9049 }
9050 }
9051 }
9052 }
9053 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9054 {
9055 tree ctor_unfilled_bitpos, elt_bitpos;
9056
9057 /* If the current record is complete we are done. */
9058 if (constructor_unfilled_fields == 0)
9059 break;
9060
9061 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9062 elt_bitpos = bit_position (elt->purpose);
9063 /* We can't compare fields here because there might be empty
9064 fields in between. */
9065 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9066 {
9067 constructor_unfilled_fields = elt->purpose;
9068 output_init_element (input_location, elt->value, elt->origtype,
9069 true, TREE_TYPE (elt->purpose),
9070 elt->purpose, 0, false,
9071 braced_init_obstack);
9072 }
9073 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9074 {
9075 /* Advance to the next smaller node. */
9076 if (elt->left)
9077 elt = elt->left;
9078 else
9079 {
9080 /* We have reached the smallest node bigger than the
9081 current unfilled field. Fill the space first. */
9082 next = elt->purpose;
9083 break;
9084 }
9085 }
9086 else
9087 {
9088 /* Advance to the next bigger node. */
9089 if (elt->right)
9090 elt = elt->right;
9091 else
9092 {
9093 /* We have reached the biggest node in a subtree. Find
9094 the parent of it, which is the next bigger node. */
9095 while (elt->parent && elt->parent->right == elt)
9096 elt = elt->parent;
9097 elt = elt->parent;
9098 if (elt
9099 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9100 bit_position (elt->purpose))))
9101 {
9102 next = elt->purpose;
9103 break;
9104 }
9105 }
9106 }
9107 }
9108 }
9109
9110 /* Ordinarily return, but not if we want to output all
9111 and there are elements left. */
9112 if (!(all && next != 0))
9113 return;
9114
9115 /* If it's not incremental, just skip over the gap, so that after
9116 jumping to retry we will output the next successive element. */
9117 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9118 constructor_unfilled_fields = next;
9119 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9120 constructor_unfilled_index = next;
9121
9122 /* ELT now points to the node in the pending tree with the next
9123 initializer to output. */
9124 goto retry;
9125 }
9126 \f
9127 /* Add one non-braced element to the current constructor level.
9128 This adjusts the current position within the constructor's type.
9129 This may also start or terminate implicit levels
9130 to handle a partly-braced initializer.
9131
9132 Once this has found the correct level for the new element,
9133 it calls output_init_element.
9134
9135 IMPLICIT is true if value comes from pop_init_level (1),
9136 the new initializer has been merged with the existing one
9137 and thus no warnings should be emitted about overriding an
9138 existing initializer. */
9139
9140 void
9141 process_init_element (location_t loc, struct c_expr value, bool implicit,
9142 struct obstack * braced_init_obstack)
9143 {
9144 tree orig_value = value.value;
9145 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
9146 bool strict_string = value.original_code == STRING_CST;
9147 bool was_designated = designator_depth != 0;
9148
9149 designator_depth = 0;
9150 designator_erroneous = 0;
9151
9152 if (!implicit && value.value && !integer_zerop (value.value))
9153 constructor_zeroinit = 0;
9154
9155 /* Handle superfluous braces around string cst as in
9156 char x[] = {"foo"}; */
9157 if (string_flag
9158 && constructor_type
9159 && !was_designated
9160 && TREE_CODE (constructor_type) == ARRAY_TYPE
9161 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9162 && integer_zerop (constructor_unfilled_index))
9163 {
9164 if (constructor_stack->replacement_value.value)
9165 error_init (loc, "excess elements in char array initializer");
9166 constructor_stack->replacement_value = value;
9167 return;
9168 }
9169
9170 if (constructor_stack->replacement_value.value != 0)
9171 {
9172 error_init (loc, "excess elements in struct initializer");
9173 return;
9174 }
9175
9176 /* Ignore elements of a brace group if it is entirely superfluous
9177 and has already been diagnosed. */
9178 if (constructor_type == 0)
9179 return;
9180
9181 if (!implicit && warn_designated_init && !was_designated
9182 && TREE_CODE (constructor_type) == RECORD_TYPE
9183 && lookup_attribute ("designated_init",
9184 TYPE_ATTRIBUTES (constructor_type)))
9185 warning_init (loc,
9186 OPT_Wdesignated_init,
9187 "positional initialization of field "
9188 "in %<struct%> declared with %<designated_init%> attribute");
9189
9190 /* If we've exhausted any levels that didn't have braces,
9191 pop them now. */
9192 while (constructor_stack->implicit)
9193 {
9194 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9195 && constructor_fields == 0)
9196 process_init_element (loc,
9197 pop_init_level (loc, 1, braced_init_obstack),
9198 true, braced_init_obstack);
9199 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9200 || VECTOR_TYPE_P (constructor_type))
9201 && constructor_max_index
9202 && tree_int_cst_lt (constructor_max_index,
9203 constructor_index))
9204 process_init_element (loc,
9205 pop_init_level (loc, 1, braced_init_obstack),
9206 true, braced_init_obstack);
9207 else
9208 break;
9209 }
9210
9211 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9212 if (constructor_range_stack)
9213 {
9214 /* If value is a compound literal and we'll be just using its
9215 content, don't put it into a SAVE_EXPR. */
9216 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9217 || !require_constant_value)
9218 {
9219 tree semantic_type = NULL_TREE;
9220 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9221 {
9222 semantic_type = TREE_TYPE (value.value);
9223 value.value = TREE_OPERAND (value.value, 0);
9224 }
9225 value.value = c_save_expr (value.value);
9226 if (semantic_type)
9227 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9228 value.value);
9229 }
9230 }
9231
9232 while (1)
9233 {
9234 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9235 {
9236 tree fieldtype;
9237 enum tree_code fieldcode;
9238
9239 if (constructor_fields == 0)
9240 {
9241 pedwarn_init (loc, 0, "excess elements in struct initializer");
9242 break;
9243 }
9244
9245 fieldtype = TREE_TYPE (constructor_fields);
9246 if (fieldtype != error_mark_node)
9247 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9248 fieldcode = TREE_CODE (fieldtype);
9249
9250 /* Error for non-static initialization of a flexible array member. */
9251 if (fieldcode == ARRAY_TYPE
9252 && !require_constant_value
9253 && TYPE_SIZE (fieldtype) == NULL_TREE
9254 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9255 {
9256 error_init (loc, "non-static initialization of a flexible "
9257 "array member");
9258 break;
9259 }
9260
9261 /* Error for initialization of a flexible array member with
9262 a string constant if the structure is in an array. E.g.:
9263 struct S { int x; char y[]; };
9264 struct S s[] = { { 1, "foo" } };
9265 is invalid. */
9266 if (string_flag
9267 && fieldcode == ARRAY_TYPE
9268 && constructor_depth > 1
9269 && TYPE_SIZE (fieldtype) == NULL_TREE
9270 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9271 {
9272 bool in_array_p = false;
9273 for (struct constructor_stack *p = constructor_stack;
9274 p && p->type; p = p->next)
9275 if (TREE_CODE (p->type) == ARRAY_TYPE)
9276 {
9277 in_array_p = true;
9278 break;
9279 }
9280 if (in_array_p)
9281 {
9282 error_init (loc, "initialization of flexible array "
9283 "member in a nested context");
9284 break;
9285 }
9286 }
9287
9288 /* Accept a string constant to initialize a subarray. */
9289 if (value.value != 0
9290 && fieldcode == ARRAY_TYPE
9291 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9292 && string_flag)
9293 value.value = orig_value;
9294 /* Otherwise, if we have come to a subaggregate,
9295 and we don't have an element of its type, push into it. */
9296 else if (value.value != 0
9297 && value.value != error_mark_node
9298 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9299 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9300 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9301 {
9302 push_init_level (loc, 1, braced_init_obstack);
9303 continue;
9304 }
9305
9306 if (value.value)
9307 {
9308 push_member_name (constructor_fields);
9309 output_init_element (loc, value.value, value.original_type,
9310 strict_string, fieldtype,
9311 constructor_fields, 1, implicit,
9312 braced_init_obstack);
9313 RESTORE_SPELLING_DEPTH (constructor_depth);
9314 }
9315 else
9316 /* Do the bookkeeping for an element that was
9317 directly output as a constructor. */
9318 {
9319 /* For a record, keep track of end position of last field. */
9320 if (DECL_SIZE (constructor_fields))
9321 constructor_bit_index
9322 = size_binop_loc (input_location, PLUS_EXPR,
9323 bit_position (constructor_fields),
9324 DECL_SIZE (constructor_fields));
9325
9326 /* If the current field was the first one not yet written out,
9327 it isn't now, so update. */
9328 if (constructor_unfilled_fields == constructor_fields)
9329 {
9330 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9331 /* Skip any nameless bit fields. */
9332 while (constructor_unfilled_fields != 0
9333 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9334 && DECL_NAME (constructor_unfilled_fields) == 0)
9335 constructor_unfilled_fields =
9336 DECL_CHAIN (constructor_unfilled_fields);
9337 }
9338 }
9339
9340 constructor_fields = DECL_CHAIN (constructor_fields);
9341 /* Skip any nameless bit fields at the beginning. */
9342 while (constructor_fields != 0
9343 && DECL_C_BIT_FIELD (constructor_fields)
9344 && DECL_NAME (constructor_fields) == 0)
9345 constructor_fields = DECL_CHAIN (constructor_fields);
9346 }
9347 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9348 {
9349 tree fieldtype;
9350 enum tree_code fieldcode;
9351
9352 if (constructor_fields == 0)
9353 {
9354 pedwarn_init (loc, 0,
9355 "excess elements in union initializer");
9356 break;
9357 }
9358
9359 fieldtype = TREE_TYPE (constructor_fields);
9360 if (fieldtype != error_mark_node)
9361 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9362 fieldcode = TREE_CODE (fieldtype);
9363
9364 /* Warn that traditional C rejects initialization of unions.
9365 We skip the warning if the value is zero. This is done
9366 under the assumption that the zero initializer in user
9367 code appears conditioned on e.g. __STDC__ to avoid
9368 "missing initializer" warnings and relies on default
9369 initialization to zero in the traditional C case.
9370 We also skip the warning if the initializer is designated,
9371 again on the assumption that this must be conditional on
9372 __STDC__ anyway (and we've already complained about the
9373 member-designator already). */
9374 if (!in_system_header_at (input_location) && !constructor_designated
9375 && !(value.value && (integer_zerop (value.value)
9376 || real_zerop (value.value))))
9377 warning (OPT_Wtraditional, "traditional C rejects initialization "
9378 "of unions");
9379
9380 /* Accept a string constant to initialize a subarray. */
9381 if (value.value != 0
9382 && fieldcode == ARRAY_TYPE
9383 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9384 && string_flag)
9385 value.value = orig_value;
9386 /* Otherwise, if we have come to a subaggregate,
9387 and we don't have an element of its type, push into it. */
9388 else if (value.value != 0
9389 && value.value != error_mark_node
9390 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9391 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9392 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9393 {
9394 push_init_level (loc, 1, braced_init_obstack);
9395 continue;
9396 }
9397
9398 if (value.value)
9399 {
9400 push_member_name (constructor_fields);
9401 output_init_element (loc, value.value, value.original_type,
9402 strict_string, fieldtype,
9403 constructor_fields, 1, implicit,
9404 braced_init_obstack);
9405 RESTORE_SPELLING_DEPTH (constructor_depth);
9406 }
9407 else
9408 /* Do the bookkeeping for an element that was
9409 directly output as a constructor. */
9410 {
9411 constructor_bit_index = DECL_SIZE (constructor_fields);
9412 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9413 }
9414
9415 constructor_fields = 0;
9416 }
9417 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9418 {
9419 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9420 enum tree_code eltcode = TREE_CODE (elttype);
9421
9422 /* Accept a string constant to initialize a subarray. */
9423 if (value.value != 0
9424 && eltcode == ARRAY_TYPE
9425 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9426 && string_flag)
9427 value.value = orig_value;
9428 /* Otherwise, if we have come to a subaggregate,
9429 and we don't have an element of its type, push into it. */
9430 else if (value.value != 0
9431 && value.value != error_mark_node
9432 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9433 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9434 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9435 {
9436 push_init_level (loc, 1, braced_init_obstack);
9437 continue;
9438 }
9439
9440 if (constructor_max_index != 0
9441 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9442 || integer_all_onesp (constructor_max_index)))
9443 {
9444 pedwarn_init (loc, 0,
9445 "excess elements in array initializer");
9446 break;
9447 }
9448
9449 /* Now output the actual element. */
9450 if (value.value)
9451 {
9452 push_array_bounds (tree_to_uhwi (constructor_index));
9453 output_init_element (loc, value.value, value.original_type,
9454 strict_string, elttype,
9455 constructor_index, 1, implicit,
9456 braced_init_obstack);
9457 RESTORE_SPELLING_DEPTH (constructor_depth);
9458 }
9459
9460 constructor_index
9461 = size_binop_loc (input_location, PLUS_EXPR,
9462 constructor_index, bitsize_one_node);
9463
9464 if (!value.value)
9465 /* If we are doing the bookkeeping for an element that was
9466 directly output as a constructor, we must update
9467 constructor_unfilled_index. */
9468 constructor_unfilled_index = constructor_index;
9469 }
9470 else if (VECTOR_TYPE_P (constructor_type))
9471 {
9472 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9473
9474 /* Do a basic check of initializer size. Note that vectors
9475 always have a fixed size derived from their type. */
9476 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9477 {
9478 pedwarn_init (loc, 0,
9479 "excess elements in vector initializer");
9480 break;
9481 }
9482
9483 /* Now output the actual element. */
9484 if (value.value)
9485 {
9486 if (TREE_CODE (value.value) == VECTOR_CST)
9487 elttype = TYPE_MAIN_VARIANT (constructor_type);
9488 output_init_element (loc, value.value, value.original_type,
9489 strict_string, elttype,
9490 constructor_index, 1, implicit,
9491 braced_init_obstack);
9492 }
9493
9494 constructor_index
9495 = size_binop_loc (input_location,
9496 PLUS_EXPR, constructor_index, bitsize_one_node);
9497
9498 if (!value.value)
9499 /* If we are doing the bookkeeping for an element that was
9500 directly output as a constructor, we must update
9501 constructor_unfilled_index. */
9502 constructor_unfilled_index = constructor_index;
9503 }
9504
9505 /* Handle the sole element allowed in a braced initializer
9506 for a scalar variable. */
9507 else if (constructor_type != error_mark_node
9508 && constructor_fields == 0)
9509 {
9510 pedwarn_init (loc, 0,
9511 "excess elements in scalar initializer");
9512 break;
9513 }
9514 else
9515 {
9516 if (value.value)
9517 output_init_element (loc, value.value, value.original_type,
9518 strict_string, constructor_type,
9519 NULL_TREE, 1, implicit,
9520 braced_init_obstack);
9521 constructor_fields = 0;
9522 }
9523
9524 /* Handle range initializers either at this level or anywhere higher
9525 in the designator stack. */
9526 if (constructor_range_stack)
9527 {
9528 struct constructor_range_stack *p, *range_stack;
9529 int finish = 0;
9530
9531 range_stack = constructor_range_stack;
9532 constructor_range_stack = 0;
9533 while (constructor_stack != range_stack->stack)
9534 {
9535 gcc_assert (constructor_stack->implicit);
9536 process_init_element (loc,
9537 pop_init_level (loc, 1,
9538 braced_init_obstack),
9539 true, braced_init_obstack);
9540 }
9541 for (p = range_stack;
9542 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9543 p = p->prev)
9544 {
9545 gcc_assert (constructor_stack->implicit);
9546 process_init_element (loc,
9547 pop_init_level (loc, 1,
9548 braced_init_obstack),
9549 true, braced_init_obstack);
9550 }
9551
9552 p->index = size_binop_loc (input_location,
9553 PLUS_EXPR, p->index, bitsize_one_node);
9554 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9555 finish = 1;
9556
9557 while (1)
9558 {
9559 constructor_index = p->index;
9560 constructor_fields = p->fields;
9561 if (finish && p->range_end && p->index == p->range_start)
9562 {
9563 finish = 0;
9564 p->prev = 0;
9565 }
9566 p = p->next;
9567 if (!p)
9568 break;
9569 finish_implicit_inits (loc, braced_init_obstack);
9570 push_init_level (loc, 2, braced_init_obstack);
9571 p->stack = constructor_stack;
9572 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9573 p->index = p->range_start;
9574 }
9575
9576 if (!finish)
9577 constructor_range_stack = range_stack;
9578 continue;
9579 }
9580
9581 break;
9582 }
9583
9584 constructor_range_stack = 0;
9585 }
9586 \f
9587 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9588 (guaranteed to be 'volatile' or null) and ARGS (represented using
9589 an ASM_EXPR node). */
9590 tree
9591 build_asm_stmt (tree cv_qualifier, tree args)
9592 {
9593 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9594 ASM_VOLATILE_P (args) = 1;
9595 return add_stmt (args);
9596 }
9597
9598 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9599 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9600 SIMPLE indicates whether there was anything at all after the
9601 string in the asm expression -- asm("blah") and asm("blah" : )
9602 are subtly different. We use a ASM_EXPR node to represent this. */
9603 tree
9604 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9605 tree clobbers, tree labels, bool simple)
9606 {
9607 tree tail;
9608 tree args;
9609 int i;
9610 const char *constraint;
9611 const char **oconstraints;
9612 bool allows_mem, allows_reg, is_inout;
9613 int ninputs, noutputs;
9614
9615 ninputs = list_length (inputs);
9616 noutputs = list_length (outputs);
9617 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9618
9619 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9620
9621 /* Remove output conversions that change the type but not the mode. */
9622 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9623 {
9624 tree output = TREE_VALUE (tail);
9625
9626 output = c_fully_fold (output, false, NULL);
9627
9628 /* ??? Really, this should not be here. Users should be using a
9629 proper lvalue, dammit. But there's a long history of using casts
9630 in the output operands. In cases like longlong.h, this becomes a
9631 primitive form of typechecking -- if the cast can be removed, then
9632 the output operand had a type of the proper width; otherwise we'll
9633 get an error. Gross, but ... */
9634 STRIP_NOPS (output);
9635
9636 if (!lvalue_or_else (loc, output, lv_asm))
9637 output = error_mark_node;
9638
9639 if (output != error_mark_node
9640 && (TREE_READONLY (output)
9641 || TYPE_READONLY (TREE_TYPE (output))
9642 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
9643 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9644 readonly_error (loc, output, lv_asm);
9645
9646 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9647 oconstraints[i] = constraint;
9648
9649 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9650 &allows_mem, &allows_reg, &is_inout))
9651 {
9652 /* If the operand is going to end up in memory,
9653 mark it addressable. */
9654 if (!allows_reg && !c_mark_addressable (output))
9655 output = error_mark_node;
9656 if (!(!allows_reg && allows_mem)
9657 && output != error_mark_node
9658 && VOID_TYPE_P (TREE_TYPE (output)))
9659 {
9660 error_at (loc, "invalid use of void expression");
9661 output = error_mark_node;
9662 }
9663 }
9664 else
9665 output = error_mark_node;
9666
9667 TREE_VALUE (tail) = output;
9668 }
9669
9670 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9671 {
9672 tree input;
9673
9674 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9675 input = TREE_VALUE (tail);
9676
9677 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9678 oconstraints, &allows_mem, &allows_reg))
9679 {
9680 /* If the operand is going to end up in memory,
9681 mark it addressable. */
9682 if (!allows_reg && allows_mem)
9683 {
9684 input = c_fully_fold (input, false, NULL);
9685
9686 /* Strip the nops as we allow this case. FIXME, this really
9687 should be rejected or made deprecated. */
9688 STRIP_NOPS (input);
9689 if (!c_mark_addressable (input))
9690 input = error_mark_node;
9691 }
9692 else
9693 {
9694 struct c_expr expr;
9695 memset (&expr, 0, sizeof (expr));
9696 expr.value = input;
9697 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9698 input = c_fully_fold (expr.value, false, NULL);
9699
9700 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9701 {
9702 error_at (loc, "invalid use of void expression");
9703 input = error_mark_node;
9704 }
9705 }
9706 }
9707 else
9708 input = error_mark_node;
9709
9710 TREE_VALUE (tail) = input;
9711 }
9712
9713 /* ASMs with labels cannot have outputs. This should have been
9714 enforced by the parser. */
9715 gcc_assert (outputs == NULL || labels == NULL);
9716
9717 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9718
9719 /* asm statements without outputs, including simple ones, are treated
9720 as volatile. */
9721 ASM_INPUT_P (args) = simple;
9722 ASM_VOLATILE_P (args) = (noutputs == 0);
9723
9724 return args;
9725 }
9726 \f
9727 /* Generate a goto statement to LABEL. LOC is the location of the
9728 GOTO. */
9729
9730 tree
9731 c_finish_goto_label (location_t loc, tree label)
9732 {
9733 tree decl = lookup_label_for_goto (loc, label);
9734 if (!decl)
9735 return NULL_TREE;
9736 TREE_USED (decl) = 1;
9737 {
9738 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9739 SET_EXPR_LOCATION (t, loc);
9740 return add_stmt (t);
9741 }
9742 }
9743
9744 /* Generate a computed goto statement to EXPR. LOC is the location of
9745 the GOTO. */
9746
9747 tree
9748 c_finish_goto_ptr (location_t loc, tree expr)
9749 {
9750 tree t;
9751 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9752 expr = c_fully_fold (expr, false, NULL);
9753 expr = convert (ptr_type_node, expr);
9754 t = build1 (GOTO_EXPR, void_type_node, expr);
9755 SET_EXPR_LOCATION (t, loc);
9756 return add_stmt (t);
9757 }
9758
9759 /* Generate a C `return' statement. RETVAL is the expression for what
9760 to return, or a null pointer for `return;' with no value. LOC is
9761 the location of the return statement, or the location of the expression,
9762 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9763 is the original type of RETVAL. */
9764
9765 tree
9766 c_finish_return (location_t loc, tree retval, tree origtype)
9767 {
9768 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9769 bool no_warning = false;
9770 bool npc = false;
9771 size_t rank = 0;
9772
9773 /* Use the expansion point to handle cases such as returning NULL
9774 in a function returning void. */
9775 source_location xloc = expansion_point_location_if_in_system_header (loc);
9776
9777 if (TREE_THIS_VOLATILE (current_function_decl))
9778 warning_at (xloc, 0,
9779 "function declared %<noreturn%> has a %<return%> statement");
9780
9781 if (flag_cilkplus && contains_array_notation_expr (retval))
9782 {
9783 /* Array notations are allowed in a return statement if it is inside a
9784 built-in array notation reduction function. */
9785 if (!find_rank (loc, retval, retval, false, &rank))
9786 return error_mark_node;
9787 if (rank >= 1)
9788 {
9789 error_at (loc, "array notation expression cannot be used as a "
9790 "return value");
9791 return error_mark_node;
9792 }
9793 }
9794 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9795 {
9796 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9797 "allowed");
9798 return error_mark_node;
9799 }
9800 if (retval)
9801 {
9802 tree semantic_type = NULL_TREE;
9803 npc = null_pointer_constant_p (retval);
9804 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9805 {
9806 semantic_type = TREE_TYPE (retval);
9807 retval = TREE_OPERAND (retval, 0);
9808 }
9809 retval = c_fully_fold (retval, false, NULL);
9810 if (semantic_type)
9811 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9812 }
9813
9814 if (!retval)
9815 {
9816 current_function_returns_null = 1;
9817 if ((warn_return_type || flag_isoc99)
9818 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9819 {
9820 bool warned_here;
9821 if (flag_isoc99)
9822 warned_here = pedwarn
9823 (loc, 0,
9824 "%<return%> with no value, in function returning non-void");
9825 else
9826 warned_here = warning_at
9827 (loc, OPT_Wreturn_type,
9828 "%<return%> with no value, in function returning non-void");
9829 no_warning = true;
9830 if (warned_here)
9831 inform (DECL_SOURCE_LOCATION (current_function_decl),
9832 "declared here");
9833 }
9834 }
9835 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9836 {
9837 current_function_returns_null = 1;
9838 bool warned_here;
9839 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9840 warned_here = pedwarn
9841 (xloc, 0,
9842 "%<return%> with a value, in function returning void");
9843 else
9844 warned_here = pedwarn
9845 (xloc, OPT_Wpedantic, "ISO C forbids "
9846 "%<return%> with expression, in function returning void");
9847 if (warned_here)
9848 inform (DECL_SOURCE_LOCATION (current_function_decl),
9849 "declared here");
9850 }
9851 else
9852 {
9853 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9854 retval, origtype, ic_return,
9855 npc, NULL_TREE, NULL_TREE, 0);
9856 tree res = DECL_RESULT (current_function_decl);
9857 tree inner;
9858 bool save;
9859
9860 current_function_returns_value = 1;
9861 if (t == error_mark_node)
9862 return NULL_TREE;
9863
9864 save = in_late_binary_op;
9865 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9866 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9867 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9868 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9869 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9870 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9871 in_late_binary_op = true;
9872 inner = t = convert (TREE_TYPE (res), t);
9873 in_late_binary_op = save;
9874
9875 /* Strip any conversions, additions, and subtractions, and see if
9876 we are returning the address of a local variable. Warn if so. */
9877 while (1)
9878 {
9879 switch (TREE_CODE (inner))
9880 {
9881 CASE_CONVERT:
9882 case NON_LVALUE_EXPR:
9883 case PLUS_EXPR:
9884 case POINTER_PLUS_EXPR:
9885 inner = TREE_OPERAND (inner, 0);
9886 continue;
9887
9888 case MINUS_EXPR:
9889 /* If the second operand of the MINUS_EXPR has a pointer
9890 type (or is converted from it), this may be valid, so
9891 don't give a warning. */
9892 {
9893 tree op1 = TREE_OPERAND (inner, 1);
9894
9895 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9896 && (CONVERT_EXPR_P (op1)
9897 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9898 op1 = TREE_OPERAND (op1, 0);
9899
9900 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9901 break;
9902
9903 inner = TREE_OPERAND (inner, 0);
9904 continue;
9905 }
9906
9907 case ADDR_EXPR:
9908 inner = TREE_OPERAND (inner, 0);
9909
9910 while (REFERENCE_CLASS_P (inner)
9911 && !INDIRECT_REF_P (inner))
9912 inner = TREE_OPERAND (inner, 0);
9913
9914 if (DECL_P (inner)
9915 && !DECL_EXTERNAL (inner)
9916 && !TREE_STATIC (inner)
9917 && DECL_CONTEXT (inner) == current_function_decl)
9918 {
9919 if (TREE_CODE (inner) == LABEL_DECL)
9920 warning_at (loc, OPT_Wreturn_local_addr,
9921 "function returns address of label");
9922 else
9923 {
9924 warning_at (loc, OPT_Wreturn_local_addr,
9925 "function returns address of local variable");
9926 tree zero = build_zero_cst (TREE_TYPE (res));
9927 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9928 }
9929 }
9930 break;
9931
9932 default:
9933 break;
9934 }
9935
9936 break;
9937 }
9938
9939 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9940 SET_EXPR_LOCATION (retval, loc);
9941
9942 if (warn_sequence_point)
9943 verify_sequence_points (retval);
9944 }
9945
9946 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9947 TREE_NO_WARNING (ret_stmt) |= no_warning;
9948 return add_stmt (ret_stmt);
9949 }
9950 \f
9951 struct c_switch {
9952 /* The SWITCH_EXPR being built. */
9953 tree switch_expr;
9954
9955 /* The original type of the testing expression, i.e. before the
9956 default conversion is applied. */
9957 tree orig_type;
9958
9959 /* A splay-tree mapping the low element of a case range to the high
9960 element, or NULL_TREE if there is no high element. Used to
9961 determine whether or not a new case label duplicates an old case
9962 label. We need a tree, rather than simply a hash table, because
9963 of the GNU case range extension. */
9964 splay_tree cases;
9965
9966 /* The bindings at the point of the switch. This is used for
9967 warnings crossing decls when branching to a case label. */
9968 struct c_spot_bindings *bindings;
9969
9970 /* The next node on the stack. */
9971 struct c_switch *next;
9972
9973 /* Remember whether the controlling expression had boolean type
9974 before integer promotions for the sake of -Wswitch-bool. */
9975 bool bool_cond_p;
9976
9977 /* Remember whether there was a case value that is outside the
9978 range of the ORIG_TYPE. */
9979 bool outside_range_p;
9980 };
9981
9982 /* A stack of the currently active switch statements. The innermost
9983 switch statement is on the top of the stack. There is no need to
9984 mark the stack for garbage collection because it is only active
9985 during the processing of the body of a function, and we never
9986 collect at that point. */
9987
9988 struct c_switch *c_switch_stack;
9989
9990 /* Start a C switch statement, testing expression EXP. Return the new
9991 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9992 SWITCH_COND_LOC is the location of the switch's condition.
9993 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
9994
9995 tree
9996 c_start_case (location_t switch_loc,
9997 location_t switch_cond_loc,
9998 tree exp, bool explicit_cast_p)
9999 {
10000 tree orig_type = error_mark_node;
10001 bool bool_cond_p = false;
10002 struct c_switch *cs;
10003
10004 if (exp != error_mark_node)
10005 {
10006 orig_type = TREE_TYPE (exp);
10007
10008 if (!INTEGRAL_TYPE_P (orig_type))
10009 {
10010 if (orig_type != error_mark_node)
10011 {
10012 error_at (switch_cond_loc, "switch quantity not an integer");
10013 orig_type = error_mark_node;
10014 }
10015 exp = integer_zero_node;
10016 }
10017 else
10018 {
10019 tree type = TYPE_MAIN_VARIANT (orig_type);
10020 tree e = exp;
10021
10022 /* Warn if the condition has boolean value. */
10023 while (TREE_CODE (e) == COMPOUND_EXPR)
10024 e = TREE_OPERAND (e, 1);
10025
10026 if ((TREE_CODE (type) == BOOLEAN_TYPE
10027 || truth_value_p (TREE_CODE (e)))
10028 /* Explicit cast to int suppresses this warning. */
10029 && !(TREE_CODE (type) == INTEGER_TYPE
10030 && explicit_cast_p))
10031 bool_cond_p = true;
10032
10033 if (!in_system_header_at (input_location)
10034 && (type == long_integer_type_node
10035 || type == long_unsigned_type_node))
10036 warning_at (switch_cond_loc,
10037 OPT_Wtraditional, "%<long%> switch expression not "
10038 "converted to %<int%> in ISO C");
10039
10040 exp = c_fully_fold (exp, false, NULL);
10041 exp = default_conversion (exp);
10042
10043 if (warn_sequence_point)
10044 verify_sequence_points (exp);
10045 }
10046 }
10047
10048 /* Add this new SWITCH_EXPR to the stack. */
10049 cs = XNEW (struct c_switch);
10050 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
10051 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10052 cs->orig_type = orig_type;
10053 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10054 cs->bindings = c_get_switch_bindings ();
10055 cs->bool_cond_p = bool_cond_p;
10056 cs->outside_range_p = false;
10057 cs->next = c_switch_stack;
10058 c_switch_stack = cs;
10059
10060 return add_stmt (cs->switch_expr);
10061 }
10062
10063 /* Process a case label at location LOC. */
10064
10065 tree
10066 do_case (location_t loc, tree low_value, tree high_value)
10067 {
10068 tree label = NULL_TREE;
10069
10070 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10071 {
10072 low_value = c_fully_fold (low_value, false, NULL);
10073 if (TREE_CODE (low_value) == INTEGER_CST)
10074 pedwarn (loc, OPT_Wpedantic,
10075 "case label is not an integer constant expression");
10076 }
10077
10078 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10079 {
10080 high_value = c_fully_fold (high_value, false, NULL);
10081 if (TREE_CODE (high_value) == INTEGER_CST)
10082 pedwarn (input_location, OPT_Wpedantic,
10083 "case label is not an integer constant expression");
10084 }
10085
10086 if (c_switch_stack == NULL)
10087 {
10088 if (low_value)
10089 error_at (loc, "case label not within a switch statement");
10090 else
10091 error_at (loc, "%<default%> label not within a switch statement");
10092 return NULL_TREE;
10093 }
10094
10095 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10096 EXPR_LOCATION (c_switch_stack->switch_expr),
10097 loc))
10098 return NULL_TREE;
10099
10100 label = c_add_case_label (loc, c_switch_stack->cases,
10101 SWITCH_COND (c_switch_stack->switch_expr),
10102 c_switch_stack->orig_type,
10103 low_value, high_value,
10104 &c_switch_stack->outside_range_p);
10105 if (label == error_mark_node)
10106 label = NULL_TREE;
10107 return label;
10108 }
10109
10110 /* Finish the switch statement. TYPE is the original type of the
10111 controlling expression of the switch, or NULL_TREE. */
10112
10113 void
10114 c_finish_case (tree body, tree type)
10115 {
10116 struct c_switch *cs = c_switch_stack;
10117 location_t switch_location;
10118
10119 SWITCH_BODY (cs->switch_expr) = body;
10120
10121 /* Emit warnings as needed. */
10122 switch_location = EXPR_LOCATION (cs->switch_expr);
10123 c_do_switch_warnings (cs->cases, switch_location,
10124 type ? type : TREE_TYPE (cs->switch_expr),
10125 SWITCH_COND (cs->switch_expr),
10126 cs->bool_cond_p, cs->outside_range_p);
10127
10128 /* Pop the stack. */
10129 c_switch_stack = cs->next;
10130 splay_tree_delete (cs->cases);
10131 c_release_switch_bindings (cs->bindings);
10132 XDELETE (cs);
10133 }
10134 \f
10135 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10136 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10137 may be null. */
10138
10139 void
10140 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10141 tree else_block)
10142 {
10143 tree stmt;
10144
10145 /* If the condition has array notations, then the rank of the then_block and
10146 else_block must be either 0 or be equal to the rank of the condition. If
10147 the condition does not have array notations then break them up as it is
10148 broken up in a normal expression. */
10149 if (flag_cilkplus && contains_array_notation_expr (cond))
10150 {
10151 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10152 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10153 return;
10154 if (then_block
10155 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10156 return;
10157 if (else_block
10158 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10159 return;
10160 if (cond_rank != then_rank && then_rank != 0)
10161 {
10162 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10163 " and the then-block");
10164 return;
10165 }
10166 else if (cond_rank != else_rank && else_rank != 0)
10167 {
10168 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10169 " and the else-block");
10170 return;
10171 }
10172 }
10173
10174 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10175 SET_EXPR_LOCATION (stmt, if_locus);
10176 add_stmt (stmt);
10177 }
10178
10179 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10180 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10181 is false for DO loops. INCR is the FOR increment expression. BODY is
10182 the statement controlled by the loop. BLAB is the break label. CLAB is
10183 the continue label. Everything is allowed to be NULL. */
10184
10185 void
10186 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10187 tree blab, tree clab, bool cond_is_first)
10188 {
10189 tree entry = NULL, exit = NULL, t;
10190
10191 /* In theory could forbid cilk spawn for loop increment expression,
10192 but it should work just fine. */
10193
10194 /* If the condition is zero don't generate a loop construct. */
10195 if (cond && integer_zerop (cond))
10196 {
10197 if (cond_is_first)
10198 {
10199 t = build_and_jump (&blab);
10200 SET_EXPR_LOCATION (t, start_locus);
10201 add_stmt (t);
10202 }
10203 }
10204 else
10205 {
10206 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10207
10208 /* If we have an exit condition, then we build an IF with gotos either
10209 out of the loop, or to the top of it. If there's no exit condition,
10210 then we just build a jump back to the top. */
10211 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10212
10213 if (cond && !integer_nonzerop (cond))
10214 {
10215 /* Canonicalize the loop condition to the end. This means
10216 generating a branch to the loop condition. Reuse the
10217 continue label, if possible. */
10218 if (cond_is_first)
10219 {
10220 if (incr || !clab)
10221 {
10222 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10223 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10224 }
10225 else
10226 t = build1 (GOTO_EXPR, void_type_node, clab);
10227 SET_EXPR_LOCATION (t, start_locus);
10228 add_stmt (t);
10229 }
10230
10231 t = build_and_jump (&blab);
10232 if (cond_is_first)
10233 exit = fold_build3_loc (start_locus,
10234 COND_EXPR, void_type_node, cond, exit, t);
10235 else
10236 exit = fold_build3_loc (input_location,
10237 COND_EXPR, void_type_node, cond, exit, t);
10238 }
10239 else
10240 {
10241 /* For the backward-goto's location of an unconditional loop
10242 use the beginning of the body, or, if there is none, the
10243 top of the loop. */
10244 location_t loc = EXPR_LOCATION (expr_first (body));
10245 if (loc == UNKNOWN_LOCATION)
10246 loc = start_locus;
10247 SET_EXPR_LOCATION (exit, loc);
10248 }
10249
10250 add_stmt (top);
10251 }
10252
10253 if (body)
10254 add_stmt (body);
10255 if (clab)
10256 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10257 if (incr)
10258 add_stmt (incr);
10259 if (entry)
10260 add_stmt (entry);
10261 if (exit)
10262 add_stmt (exit);
10263 if (blab)
10264 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10265 }
10266
10267 tree
10268 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10269 {
10270 bool skip;
10271 tree label = *label_p;
10272
10273 /* In switch statements break is sometimes stylistically used after
10274 a return statement. This can lead to spurious warnings about
10275 control reaching the end of a non-void function when it is
10276 inlined. Note that we are calling block_may_fallthru with
10277 language specific tree nodes; this works because
10278 block_may_fallthru returns true when given something it does not
10279 understand. */
10280 skip = !block_may_fallthru (cur_stmt_list);
10281
10282 if (!label)
10283 {
10284 if (!skip)
10285 *label_p = label = create_artificial_label (loc);
10286 }
10287 else if (TREE_CODE (label) == LABEL_DECL)
10288 ;
10289 else switch (TREE_INT_CST_LOW (label))
10290 {
10291 case 0:
10292 if (is_break)
10293 error_at (loc, "break statement not within loop or switch");
10294 else
10295 error_at (loc, "continue statement not within a loop");
10296 return NULL_TREE;
10297
10298 case 1:
10299 gcc_assert (is_break);
10300 error_at (loc, "break statement used with OpenMP for loop");
10301 return NULL_TREE;
10302
10303 case 2:
10304 if (is_break)
10305 error ("break statement within %<#pragma simd%> loop body");
10306 else
10307 error ("continue statement within %<#pragma simd%> loop body");
10308 return NULL_TREE;
10309
10310 default:
10311 gcc_unreachable ();
10312 }
10313
10314 if (skip)
10315 return NULL_TREE;
10316
10317 if (!is_break)
10318 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10319
10320 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10321 }
10322
10323 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10324
10325 static void
10326 emit_side_effect_warnings (location_t loc, tree expr)
10327 {
10328 if (expr == error_mark_node)
10329 ;
10330 else if (!TREE_SIDE_EFFECTS (expr))
10331 {
10332 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10333 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10334 }
10335 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10336 {
10337 tree r = expr;
10338 location_t cloc = loc;
10339 while (TREE_CODE (r) == COMPOUND_EXPR)
10340 {
10341 if (EXPR_HAS_LOCATION (r))
10342 cloc = EXPR_LOCATION (r);
10343 r = TREE_OPERAND (r, 1);
10344 }
10345 if (!TREE_SIDE_EFFECTS (r)
10346 && !VOID_TYPE_P (TREE_TYPE (r))
10347 && !CONVERT_EXPR_P (r)
10348 && !TREE_NO_WARNING (r)
10349 && !TREE_NO_WARNING (expr))
10350 warning_at (cloc, OPT_Wunused_value,
10351 "right-hand operand of comma expression has no effect");
10352 }
10353 else
10354 warn_if_unused_value (expr, loc);
10355 }
10356
10357 /* Process an expression as if it were a complete statement. Emit
10358 diagnostics, but do not call ADD_STMT. LOC is the location of the
10359 statement. */
10360
10361 tree
10362 c_process_expr_stmt (location_t loc, tree expr)
10363 {
10364 tree exprv;
10365
10366 if (!expr)
10367 return NULL_TREE;
10368
10369 expr = c_fully_fold (expr, false, NULL);
10370
10371 if (warn_sequence_point)
10372 verify_sequence_points (expr);
10373
10374 if (TREE_TYPE (expr) != error_mark_node
10375 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10376 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10377 error_at (loc, "expression statement has incomplete type");
10378
10379 /* If we're not processing a statement expression, warn about unused values.
10380 Warnings for statement expressions will be emitted later, once we figure
10381 out which is the result. */
10382 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10383 && warn_unused_value)
10384 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10385
10386 exprv = expr;
10387 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10388 exprv = TREE_OPERAND (exprv, 1);
10389 while (CONVERT_EXPR_P (exprv))
10390 exprv = TREE_OPERAND (exprv, 0);
10391 if (DECL_P (exprv)
10392 || handled_component_p (exprv)
10393 || TREE_CODE (exprv) == ADDR_EXPR)
10394 mark_exp_read (exprv);
10395
10396 /* If the expression is not of a type to which we cannot assign a line
10397 number, wrap the thing in a no-op NOP_EXPR. */
10398 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10399 {
10400 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10401 SET_EXPR_LOCATION (expr, loc);
10402 }
10403
10404 return expr;
10405 }
10406
10407 /* Emit an expression as a statement. LOC is the location of the
10408 expression. */
10409
10410 tree
10411 c_finish_expr_stmt (location_t loc, tree expr)
10412 {
10413 if (expr)
10414 return add_stmt (c_process_expr_stmt (loc, expr));
10415 else
10416 return NULL;
10417 }
10418
10419 /* Do the opposite and emit a statement as an expression. To begin,
10420 create a new binding level and return it. */
10421
10422 tree
10423 c_begin_stmt_expr (void)
10424 {
10425 tree ret;
10426
10427 /* We must force a BLOCK for this level so that, if it is not expanded
10428 later, there is a way to turn off the entire subtree of blocks that
10429 are contained in it. */
10430 keep_next_level ();
10431 ret = c_begin_compound_stmt (true);
10432
10433 c_bindings_start_stmt_expr (c_switch_stack == NULL
10434 ? NULL
10435 : c_switch_stack->bindings);
10436
10437 /* Mark the current statement list as belonging to a statement list. */
10438 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10439
10440 return ret;
10441 }
10442
10443 /* LOC is the location of the compound statement to which this body
10444 belongs. */
10445
10446 tree
10447 c_finish_stmt_expr (location_t loc, tree body)
10448 {
10449 tree last, type, tmp, val;
10450 tree *last_p;
10451
10452 body = c_end_compound_stmt (loc, body, true);
10453
10454 c_bindings_end_stmt_expr (c_switch_stack == NULL
10455 ? NULL
10456 : c_switch_stack->bindings);
10457
10458 /* Locate the last statement in BODY. See c_end_compound_stmt
10459 about always returning a BIND_EXPR. */
10460 last_p = &BIND_EXPR_BODY (body);
10461 last = BIND_EXPR_BODY (body);
10462
10463 continue_searching:
10464 if (TREE_CODE (last) == STATEMENT_LIST)
10465 {
10466 tree_stmt_iterator i;
10467
10468 /* This can happen with degenerate cases like ({ }). No value. */
10469 if (!TREE_SIDE_EFFECTS (last))
10470 return body;
10471
10472 /* If we're supposed to generate side effects warnings, process
10473 all of the statements except the last. */
10474 if (warn_unused_value)
10475 {
10476 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10477 {
10478 location_t tloc;
10479 tree t = tsi_stmt (i);
10480
10481 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10482 emit_side_effect_warnings (tloc, t);
10483 }
10484 }
10485 else
10486 i = tsi_last (last);
10487 last_p = tsi_stmt_ptr (i);
10488 last = *last_p;
10489 }
10490
10491 /* If the end of the list is exception related, then the list was split
10492 by a call to push_cleanup. Continue searching. */
10493 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10494 || TREE_CODE (last) == TRY_CATCH_EXPR)
10495 {
10496 last_p = &TREE_OPERAND (last, 0);
10497 last = *last_p;
10498 goto continue_searching;
10499 }
10500
10501 if (last == error_mark_node)
10502 return last;
10503
10504 /* In the case that the BIND_EXPR is not necessary, return the
10505 expression out from inside it. */
10506 if (last == BIND_EXPR_BODY (body)
10507 && BIND_EXPR_VARS (body) == NULL)
10508 {
10509 /* Even if this looks constant, do not allow it in a constant
10510 expression. */
10511 last = c_wrap_maybe_const (last, true);
10512 /* Do not warn if the return value of a statement expression is
10513 unused. */
10514 TREE_NO_WARNING (last) = 1;
10515 return last;
10516 }
10517
10518 /* Extract the type of said expression. */
10519 type = TREE_TYPE (last);
10520
10521 /* If we're not returning a value at all, then the BIND_EXPR that
10522 we already have is a fine expression to return. */
10523 if (!type || VOID_TYPE_P (type))
10524 return body;
10525
10526 /* Now that we've located the expression containing the value, it seems
10527 silly to make voidify_wrapper_expr repeat the process. Create a
10528 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10529 tmp = create_tmp_var_raw (type);
10530
10531 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10532 tree_expr_nonnegative_p giving up immediately. */
10533 val = last;
10534 if (TREE_CODE (val) == NOP_EXPR
10535 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10536 val = TREE_OPERAND (val, 0);
10537
10538 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10539 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10540
10541 {
10542 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10543 SET_EXPR_LOCATION (t, loc);
10544 return t;
10545 }
10546 }
10547 \f
10548 /* Begin and end compound statements. This is as simple as pushing
10549 and popping new statement lists from the tree. */
10550
10551 tree
10552 c_begin_compound_stmt (bool do_scope)
10553 {
10554 tree stmt = push_stmt_list ();
10555 if (do_scope)
10556 push_scope ();
10557 return stmt;
10558 }
10559
10560 /* End a compound statement. STMT is the statement. LOC is the
10561 location of the compound statement-- this is usually the location
10562 of the opening brace. */
10563
10564 tree
10565 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10566 {
10567 tree block = NULL;
10568
10569 if (do_scope)
10570 {
10571 if (c_dialect_objc ())
10572 objc_clear_super_receiver ();
10573 block = pop_scope ();
10574 }
10575
10576 stmt = pop_stmt_list (stmt);
10577 stmt = c_build_bind_expr (loc, block, stmt);
10578
10579 /* If this compound statement is nested immediately inside a statement
10580 expression, then force a BIND_EXPR to be created. Otherwise we'll
10581 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10582 STATEMENT_LISTs merge, and thus we can lose track of what statement
10583 was really last. */
10584 if (building_stmt_list_p ()
10585 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10586 && TREE_CODE (stmt) != BIND_EXPR)
10587 {
10588 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10589 TREE_SIDE_EFFECTS (stmt) = 1;
10590 SET_EXPR_LOCATION (stmt, loc);
10591 }
10592
10593 return stmt;
10594 }
10595
10596 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10597 when the current scope is exited. EH_ONLY is true when this is not
10598 meant to apply to normal control flow transfer. */
10599
10600 void
10601 push_cleanup (tree decl, tree cleanup, bool eh_only)
10602 {
10603 enum tree_code code;
10604 tree stmt, list;
10605 bool stmt_expr;
10606
10607 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10608 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10609 add_stmt (stmt);
10610 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10611 list = push_stmt_list ();
10612 TREE_OPERAND (stmt, 0) = list;
10613 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10614 }
10615 \f
10616 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10617 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10618
10619 static tree
10620 build_vec_cmp (tree_code code, tree type,
10621 tree arg0, tree arg1)
10622 {
10623 tree zero_vec = build_zero_cst (type);
10624 tree minus_one_vec = build_minus_one_cst (type);
10625 tree cmp_type = build_same_sized_truth_vector_type (type);
10626 tree cmp = build2 (code, cmp_type, arg0, arg1);
10627 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10628 }
10629
10630 /* Build a binary-operation expression without default conversions.
10631 CODE is the kind of expression to build.
10632 LOCATION is the operator's location.
10633 This function differs from `build' in several ways:
10634 the data type of the result is computed and recorded in it,
10635 warnings are generated if arg data types are invalid,
10636 special handling for addition and subtraction of pointers is known,
10637 and some optimization is done (operations on narrow ints
10638 are done in the narrower type when that gives the same result).
10639 Constant folding is also done before the result is returned.
10640
10641 Note that the operands will never have enumeral types, or function
10642 or array types, because either they will have the default conversions
10643 performed or they have both just been converted to some other type in which
10644 the arithmetic is to be done. */
10645
10646 tree
10647 build_binary_op (location_t location, enum tree_code code,
10648 tree orig_op0, tree orig_op1, int convert_p)
10649 {
10650 tree type0, type1, orig_type0, orig_type1;
10651 tree eptype;
10652 enum tree_code code0, code1;
10653 tree op0, op1;
10654 tree ret = error_mark_node;
10655 const char *invalid_op_diag;
10656 bool op0_int_operands, op1_int_operands;
10657 bool int_const, int_const_or_overflow, int_operands;
10658
10659 /* Expression code to give to the expression when it is built.
10660 Normally this is CODE, which is what the caller asked for,
10661 but in some special cases we change it. */
10662 enum tree_code resultcode = code;
10663
10664 /* Data type in which the computation is to be performed.
10665 In the simplest cases this is the common type of the arguments. */
10666 tree result_type = NULL;
10667
10668 /* When the computation is in excess precision, the type of the
10669 final EXCESS_PRECISION_EXPR. */
10670 tree semantic_result_type = NULL;
10671
10672 /* Nonzero means operands have already been type-converted
10673 in whatever way is necessary.
10674 Zero means they need to be converted to RESULT_TYPE. */
10675 int converted = 0;
10676
10677 /* Nonzero means create the expression with this type, rather than
10678 RESULT_TYPE. */
10679 tree build_type = 0;
10680
10681 /* Nonzero means after finally constructing the expression
10682 convert it to this type. */
10683 tree final_type = 0;
10684
10685 /* Nonzero if this is an operation like MIN or MAX which can
10686 safely be computed in short if both args are promoted shorts.
10687 Also implies COMMON.
10688 -1 indicates a bitwise operation; this makes a difference
10689 in the exact conditions for when it is safe to do the operation
10690 in a narrower mode. */
10691 int shorten = 0;
10692
10693 /* Nonzero if this is a comparison operation;
10694 if both args are promoted shorts, compare the original shorts.
10695 Also implies COMMON. */
10696 int short_compare = 0;
10697
10698 /* Nonzero if this is a right-shift operation, which can be computed on the
10699 original short and then promoted if the operand is a promoted short. */
10700 int short_shift = 0;
10701
10702 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10703 int common = 0;
10704
10705 /* True means types are compatible as far as ObjC is concerned. */
10706 bool objc_ok;
10707
10708 /* True means this is an arithmetic operation that may need excess
10709 precision. */
10710 bool may_need_excess_precision;
10711
10712 /* True means this is a boolean operation that converts both its
10713 operands to truth-values. */
10714 bool boolean_op = false;
10715
10716 /* Remember whether we're doing / or %. */
10717 bool doing_div_or_mod = false;
10718
10719 /* Remember whether we're doing << or >>. */
10720 bool doing_shift = false;
10721
10722 /* Tree holding instrumentation expression. */
10723 tree instrument_expr = NULL;
10724
10725 if (location == UNKNOWN_LOCATION)
10726 location = input_location;
10727
10728 op0 = orig_op0;
10729 op1 = orig_op1;
10730
10731 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10732 if (op0_int_operands)
10733 op0 = remove_c_maybe_const_expr (op0);
10734 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10735 if (op1_int_operands)
10736 op1 = remove_c_maybe_const_expr (op1);
10737 int_operands = (op0_int_operands && op1_int_operands);
10738 if (int_operands)
10739 {
10740 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10741 && TREE_CODE (orig_op1) == INTEGER_CST);
10742 int_const = (int_const_or_overflow
10743 && !TREE_OVERFLOW (orig_op0)
10744 && !TREE_OVERFLOW (orig_op1));
10745 }
10746 else
10747 int_const = int_const_or_overflow = false;
10748
10749 /* Do not apply default conversion in mixed vector/scalar expression. */
10750 if (convert_p
10751 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
10752 {
10753 op0 = default_conversion (op0);
10754 op1 = default_conversion (op1);
10755 }
10756
10757 /* When Cilk Plus is enabled and there are array notations inside op0, then
10758 we check to see if there are builtin array notation functions. If
10759 so, then we take on the type of the array notation inside it. */
10760 if (flag_cilkplus && contains_array_notation_expr (op0))
10761 orig_type0 = type0 = find_correct_array_notation_type (op0);
10762 else
10763 orig_type0 = type0 = TREE_TYPE (op0);
10764
10765 if (flag_cilkplus && contains_array_notation_expr (op1))
10766 orig_type1 = type1 = find_correct_array_notation_type (op1);
10767 else
10768 orig_type1 = type1 = TREE_TYPE (op1);
10769
10770 /* The expression codes of the data types of the arguments tell us
10771 whether the arguments are integers, floating, pointers, etc. */
10772 code0 = TREE_CODE (type0);
10773 code1 = TREE_CODE (type1);
10774
10775 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10776 STRIP_TYPE_NOPS (op0);
10777 STRIP_TYPE_NOPS (op1);
10778
10779 /* If an error was already reported for one of the arguments,
10780 avoid reporting another error. */
10781
10782 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10783 return error_mark_node;
10784
10785 if (code0 == POINTER_TYPE
10786 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10787 return error_mark_node;
10788
10789 if (code1 == POINTER_TYPE
10790 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10791 return error_mark_node;
10792
10793 if ((invalid_op_diag
10794 = targetm.invalid_binary_op (code, type0, type1)))
10795 {
10796 error_at (location, invalid_op_diag);
10797 return error_mark_node;
10798 }
10799
10800 switch (code)
10801 {
10802 case PLUS_EXPR:
10803 case MINUS_EXPR:
10804 case MULT_EXPR:
10805 case TRUNC_DIV_EXPR:
10806 case CEIL_DIV_EXPR:
10807 case FLOOR_DIV_EXPR:
10808 case ROUND_DIV_EXPR:
10809 case EXACT_DIV_EXPR:
10810 may_need_excess_precision = true;
10811 break;
10812 default:
10813 may_need_excess_precision = false;
10814 break;
10815 }
10816 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10817 {
10818 op0 = TREE_OPERAND (op0, 0);
10819 type0 = TREE_TYPE (op0);
10820 }
10821 else if (may_need_excess_precision
10822 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10823 {
10824 type0 = eptype;
10825 op0 = convert (eptype, op0);
10826 }
10827 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10828 {
10829 op1 = TREE_OPERAND (op1, 0);
10830 type1 = TREE_TYPE (op1);
10831 }
10832 else if (may_need_excess_precision
10833 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10834 {
10835 type1 = eptype;
10836 op1 = convert (eptype, op1);
10837 }
10838
10839 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10840
10841 /* In case when one of the operands of the binary operation is
10842 a vector and another is a scalar -- convert scalar to vector. */
10843 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10844 {
10845 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10846 true);
10847
10848 switch (convert_flag)
10849 {
10850 case stv_error:
10851 return error_mark_node;
10852 case stv_firstarg:
10853 {
10854 bool maybe_const = true;
10855 tree sc;
10856 sc = c_fully_fold (op0, false, &maybe_const);
10857 sc = save_expr (sc);
10858 sc = convert (TREE_TYPE (type1), sc);
10859 op0 = build_vector_from_val (type1, sc);
10860 if (!maybe_const)
10861 op0 = c_wrap_maybe_const (op0, true);
10862 orig_type0 = type0 = TREE_TYPE (op0);
10863 code0 = TREE_CODE (type0);
10864 converted = 1;
10865 break;
10866 }
10867 case stv_secondarg:
10868 {
10869 bool maybe_const = true;
10870 tree sc;
10871 sc = c_fully_fold (op1, false, &maybe_const);
10872 sc = save_expr (sc);
10873 sc = convert (TREE_TYPE (type0), sc);
10874 op1 = build_vector_from_val (type0, sc);
10875 if (!maybe_const)
10876 op1 = c_wrap_maybe_const (op1, true);
10877 orig_type1 = type1 = TREE_TYPE (op1);
10878 code1 = TREE_CODE (type1);
10879 converted = 1;
10880 break;
10881 }
10882 default:
10883 break;
10884 }
10885 }
10886
10887 switch (code)
10888 {
10889 case PLUS_EXPR:
10890 /* Handle the pointer + int case. */
10891 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10892 {
10893 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10894 goto return_build_binary_op;
10895 }
10896 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10897 {
10898 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10899 goto return_build_binary_op;
10900 }
10901 else
10902 common = 1;
10903 break;
10904
10905 case MINUS_EXPR:
10906 /* Subtraction of two similar pointers.
10907 We must subtract them as integers, then divide by object size. */
10908 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10909 && comp_target_types (location, type0, type1))
10910 {
10911 ret = pointer_diff (location, op0, op1);
10912 goto return_build_binary_op;
10913 }
10914 /* Handle pointer minus int. Just like pointer plus int. */
10915 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10916 {
10917 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10918 goto return_build_binary_op;
10919 }
10920 else
10921 common = 1;
10922 break;
10923
10924 case MULT_EXPR:
10925 common = 1;
10926 break;
10927
10928 case TRUNC_DIV_EXPR:
10929 case CEIL_DIV_EXPR:
10930 case FLOOR_DIV_EXPR:
10931 case ROUND_DIV_EXPR:
10932 case EXACT_DIV_EXPR:
10933 doing_div_or_mod = true;
10934 warn_for_div_by_zero (location, op1);
10935
10936 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10937 || code0 == FIXED_POINT_TYPE
10938 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10939 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10940 || code1 == FIXED_POINT_TYPE
10941 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10942 {
10943 enum tree_code tcode0 = code0, tcode1 = code1;
10944
10945 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10946 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10947 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10948 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10949
10950 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10951 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10952 resultcode = RDIV_EXPR;
10953 else
10954 /* Although it would be tempting to shorten always here, that
10955 loses on some targets, since the modulo instruction is
10956 undefined if the quotient can't be represented in the
10957 computation mode. We shorten only if unsigned or if
10958 dividing by something we know != -1. */
10959 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10960 || (TREE_CODE (op1) == INTEGER_CST
10961 && !integer_all_onesp (op1)));
10962 common = 1;
10963 }
10964 break;
10965
10966 case BIT_AND_EXPR:
10967 case BIT_IOR_EXPR:
10968 case BIT_XOR_EXPR:
10969 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10970 shorten = -1;
10971 /* Allow vector types which are not floating point types. */
10972 else if (code0 == VECTOR_TYPE
10973 && code1 == VECTOR_TYPE
10974 && !VECTOR_FLOAT_TYPE_P (type0)
10975 && !VECTOR_FLOAT_TYPE_P (type1))
10976 common = 1;
10977 break;
10978
10979 case TRUNC_MOD_EXPR:
10980 case FLOOR_MOD_EXPR:
10981 doing_div_or_mod = true;
10982 warn_for_div_by_zero (location, op1);
10983
10984 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10985 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10986 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10987 common = 1;
10988 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10989 {
10990 /* Although it would be tempting to shorten always here, that loses
10991 on some targets, since the modulo instruction is undefined if the
10992 quotient can't be represented in the computation mode. We shorten
10993 only if unsigned or if dividing by something we know != -1. */
10994 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10995 || (TREE_CODE (op1) == INTEGER_CST
10996 && !integer_all_onesp (op1)));
10997 common = 1;
10998 }
10999 break;
11000
11001 case TRUTH_ANDIF_EXPR:
11002 case TRUTH_ORIF_EXPR:
11003 case TRUTH_AND_EXPR:
11004 case TRUTH_OR_EXPR:
11005 case TRUTH_XOR_EXPR:
11006 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11007 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11008 || code0 == FIXED_POINT_TYPE)
11009 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11010 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11011 || code1 == FIXED_POINT_TYPE))
11012 {
11013 /* Result of these operations is always an int,
11014 but that does not mean the operands should be
11015 converted to ints! */
11016 result_type = integer_type_node;
11017 if (op0_int_operands)
11018 {
11019 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11020 op0 = remove_c_maybe_const_expr (op0);
11021 }
11022 else
11023 op0 = c_objc_common_truthvalue_conversion (location, op0);
11024 if (op1_int_operands)
11025 {
11026 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11027 op1 = remove_c_maybe_const_expr (op1);
11028 }
11029 else
11030 op1 = c_objc_common_truthvalue_conversion (location, op1);
11031 converted = 1;
11032 boolean_op = true;
11033 }
11034 if (code == TRUTH_ANDIF_EXPR)
11035 {
11036 int_const_or_overflow = (int_operands
11037 && TREE_CODE (orig_op0) == INTEGER_CST
11038 && (op0 == truthvalue_false_node
11039 || TREE_CODE (orig_op1) == INTEGER_CST));
11040 int_const = (int_const_or_overflow
11041 && !TREE_OVERFLOW (orig_op0)
11042 && (op0 == truthvalue_false_node
11043 || !TREE_OVERFLOW (orig_op1)));
11044 }
11045 else if (code == TRUTH_ORIF_EXPR)
11046 {
11047 int_const_or_overflow = (int_operands
11048 && TREE_CODE (orig_op0) == INTEGER_CST
11049 && (op0 == truthvalue_true_node
11050 || TREE_CODE (orig_op1) == INTEGER_CST));
11051 int_const = (int_const_or_overflow
11052 && !TREE_OVERFLOW (orig_op0)
11053 && (op0 == truthvalue_true_node
11054 || !TREE_OVERFLOW (orig_op1)));
11055 }
11056 break;
11057
11058 /* Shift operations: result has same type as first operand;
11059 always convert second operand to int.
11060 Also set SHORT_SHIFT if shifting rightward. */
11061
11062 case RSHIFT_EXPR:
11063 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11064 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11065 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11066 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11067 {
11068 result_type = type0;
11069 converted = 1;
11070 }
11071 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11072 || code0 == VECTOR_TYPE)
11073 && code1 == INTEGER_TYPE)
11074 {
11075 doing_shift = true;
11076 if (TREE_CODE (op1) == INTEGER_CST)
11077 {
11078 if (tree_int_cst_sgn (op1) < 0)
11079 {
11080 int_const = false;
11081 if (c_inhibit_evaluation_warnings == 0)
11082 warning_at (location, OPT_Wshift_count_negative,
11083 "right shift count is negative");
11084 }
11085 else if (code0 == VECTOR_TYPE)
11086 {
11087 if (compare_tree_int (op1,
11088 TYPE_PRECISION (TREE_TYPE (type0)))
11089 >= 0)
11090 {
11091 int_const = false;
11092 if (c_inhibit_evaluation_warnings == 0)
11093 warning_at (location, OPT_Wshift_count_overflow,
11094 "right shift count >= width of vector element");
11095 }
11096 }
11097 else
11098 {
11099 if (!integer_zerop (op1))
11100 short_shift = 1;
11101
11102 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11103 {
11104 int_const = false;
11105 if (c_inhibit_evaluation_warnings == 0)
11106 warning_at (location, OPT_Wshift_count_overflow,
11107 "right shift count >= width of type");
11108 }
11109 }
11110 }
11111
11112 /* Use the type of the value to be shifted. */
11113 result_type = type0;
11114 /* Avoid converting op1 to result_type later. */
11115 converted = 1;
11116 }
11117 break;
11118
11119 case LSHIFT_EXPR:
11120 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11121 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11122 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11123 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11124 {
11125 result_type = type0;
11126 converted = 1;
11127 }
11128 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11129 || code0 == VECTOR_TYPE)
11130 && code1 == INTEGER_TYPE)
11131 {
11132 doing_shift = true;
11133 if (TREE_CODE (op0) == INTEGER_CST
11134 && tree_int_cst_sgn (op0) < 0)
11135 {
11136 /* Don't reject a left shift of a negative value in a context
11137 where a constant expression is needed in C90. */
11138 if (flag_isoc99)
11139 int_const = false;
11140 if (c_inhibit_evaluation_warnings == 0)
11141 warning_at (location, OPT_Wshift_negative_value,
11142 "left shift of negative value");
11143 }
11144 if (TREE_CODE (op1) == INTEGER_CST)
11145 {
11146 if (tree_int_cst_sgn (op1) < 0)
11147 {
11148 int_const = false;
11149 if (c_inhibit_evaluation_warnings == 0)
11150 warning_at (location, OPT_Wshift_count_negative,
11151 "left shift count is negative");
11152 }
11153 else if (code0 == VECTOR_TYPE)
11154 {
11155 if (compare_tree_int (op1,
11156 TYPE_PRECISION (TREE_TYPE (type0)))
11157 >= 0)
11158 {
11159 int_const = false;
11160 if (c_inhibit_evaluation_warnings == 0)
11161 warning_at (location, OPT_Wshift_count_overflow,
11162 "left shift count >= width of vector element");
11163 }
11164 }
11165 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11166 {
11167 int_const = false;
11168 if (c_inhibit_evaluation_warnings == 0)
11169 warning_at (location, OPT_Wshift_count_overflow,
11170 "left shift count >= width of type");
11171 }
11172 else if (TREE_CODE (op0) == INTEGER_CST
11173 && maybe_warn_shift_overflow (location, op0, op1)
11174 && flag_isoc99)
11175 int_const = false;
11176 }
11177
11178 /* Use the type of the value to be shifted. */
11179 result_type = type0;
11180 /* Avoid converting op1 to result_type later. */
11181 converted = 1;
11182 }
11183 break;
11184
11185 case EQ_EXPR:
11186 case NE_EXPR:
11187 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11188 {
11189 tree intt;
11190 if (!vector_types_compatible_elements_p (type0, type1))
11191 {
11192 error_at (location, "comparing vectors with different "
11193 "element types");
11194 return error_mark_node;
11195 }
11196
11197 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11198 {
11199 error_at (location, "comparing vectors with different "
11200 "number of elements");
11201 return error_mark_node;
11202 }
11203
11204 /* It's not precisely specified how the usual arithmetic
11205 conversions apply to the vector types. Here, we use
11206 the unsigned type if one of the operands is signed and
11207 the other one is unsigned. */
11208 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11209 {
11210 if (!TYPE_UNSIGNED (type0))
11211 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11212 else
11213 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11214 warning_at (location, OPT_Wsign_compare, "comparison between "
11215 "types %qT and %qT", type0, type1);
11216 }
11217
11218 /* Always construct signed integer vector type. */
11219 intt = c_common_type_for_size (GET_MODE_BITSIZE
11220 (TYPE_MODE (TREE_TYPE (type0))), 0);
11221 result_type = build_opaque_vector_type (intt,
11222 TYPE_VECTOR_SUBPARTS (type0));
11223 converted = 1;
11224 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11225 goto return_build_binary_op;
11226 }
11227 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11228 warning_at (location,
11229 OPT_Wfloat_equal,
11230 "comparing floating point with == or != is unsafe");
11231 /* Result of comparison is always int,
11232 but don't convert the args to int! */
11233 build_type = integer_type_node;
11234 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11235 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11236 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11237 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11238 short_compare = 1;
11239 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11240 {
11241 if (TREE_CODE (op0) == ADDR_EXPR
11242 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11243 && !from_macro_expansion_at (location))
11244 {
11245 if (code == EQ_EXPR)
11246 warning_at (location,
11247 OPT_Waddress,
11248 "the comparison will always evaluate as %<false%> "
11249 "for the address of %qD will never be NULL",
11250 TREE_OPERAND (op0, 0));
11251 else
11252 warning_at (location,
11253 OPT_Waddress,
11254 "the comparison will always evaluate as %<true%> "
11255 "for the address of %qD will never be NULL",
11256 TREE_OPERAND (op0, 0));
11257 }
11258 result_type = type0;
11259 }
11260 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11261 {
11262 if (TREE_CODE (op1) == ADDR_EXPR
11263 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11264 && !from_macro_expansion_at (location))
11265 {
11266 if (code == EQ_EXPR)
11267 warning_at (location,
11268 OPT_Waddress,
11269 "the comparison will always evaluate as %<false%> "
11270 "for the address of %qD will never be NULL",
11271 TREE_OPERAND (op1, 0));
11272 else
11273 warning_at (location,
11274 OPT_Waddress,
11275 "the comparison will always evaluate as %<true%> "
11276 "for the address of %qD will never be NULL",
11277 TREE_OPERAND (op1, 0));
11278 }
11279 result_type = type1;
11280 }
11281 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11282 {
11283 tree tt0 = TREE_TYPE (type0);
11284 tree tt1 = TREE_TYPE (type1);
11285 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11286 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11287 addr_space_t as_common = ADDR_SPACE_GENERIC;
11288
11289 /* Anything compares with void *. void * compares with anything.
11290 Otherwise, the targets must be compatible
11291 and both must be object or both incomplete. */
11292 if (comp_target_types (location, type0, type1))
11293 result_type = common_pointer_type (type0, type1);
11294 else if (!addr_space_superset (as0, as1, &as_common))
11295 {
11296 error_at (location, "comparison of pointers to "
11297 "disjoint address spaces");
11298 return error_mark_node;
11299 }
11300 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11301 {
11302 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11303 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11304 "comparison of %<void *%> with function pointer");
11305 }
11306 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11307 {
11308 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11309 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11310 "comparison of %<void *%> with function pointer");
11311 }
11312 else
11313 /* Avoid warning about the volatile ObjC EH puts on decls. */
11314 if (!objc_ok)
11315 pedwarn (location, 0,
11316 "comparison of distinct pointer types lacks a cast");
11317
11318 if (result_type == NULL_TREE)
11319 {
11320 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11321 result_type = build_pointer_type
11322 (build_qualified_type (void_type_node, qual));
11323 }
11324 }
11325 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11326 {
11327 result_type = type0;
11328 pedwarn (location, 0, "comparison between pointer and integer");
11329 }
11330 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11331 {
11332 result_type = type1;
11333 pedwarn (location, 0, "comparison between pointer and integer");
11334 }
11335 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11336 || truth_value_p (TREE_CODE (orig_op0)))
11337 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11338 || truth_value_p (TREE_CODE (orig_op1))))
11339 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11340 break;
11341
11342 case LE_EXPR:
11343 case GE_EXPR:
11344 case LT_EXPR:
11345 case GT_EXPR:
11346 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11347 {
11348 tree intt;
11349 if (!vector_types_compatible_elements_p (type0, type1))
11350 {
11351 error_at (location, "comparing vectors with different "
11352 "element types");
11353 return error_mark_node;
11354 }
11355
11356 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11357 {
11358 error_at (location, "comparing vectors with different "
11359 "number of elements");
11360 return error_mark_node;
11361 }
11362
11363 /* It's not precisely specified how the usual arithmetic
11364 conversions apply to the vector types. Here, we use
11365 the unsigned type if one of the operands is signed and
11366 the other one is unsigned. */
11367 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11368 {
11369 if (!TYPE_UNSIGNED (type0))
11370 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11371 else
11372 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11373 warning_at (location, OPT_Wsign_compare, "comparison between "
11374 "types %qT and %qT", type0, type1);
11375 }
11376
11377 /* Always construct signed integer vector type. */
11378 intt = c_common_type_for_size (GET_MODE_BITSIZE
11379 (TYPE_MODE (TREE_TYPE (type0))), 0);
11380 result_type = build_opaque_vector_type (intt,
11381 TYPE_VECTOR_SUBPARTS (type0));
11382 converted = 1;
11383 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11384 goto return_build_binary_op;
11385 }
11386 build_type = integer_type_node;
11387 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11388 || code0 == FIXED_POINT_TYPE)
11389 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11390 || code1 == FIXED_POINT_TYPE))
11391 short_compare = 1;
11392 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11393 {
11394 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11395 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11396 addr_space_t as_common;
11397
11398 if (comp_target_types (location, type0, type1))
11399 {
11400 result_type = common_pointer_type (type0, type1);
11401 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11402 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11403 pedwarn (location, 0,
11404 "comparison of complete and incomplete pointers");
11405 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11406 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11407 "ordered comparisons of pointers to functions");
11408 else if (null_pointer_constant_p (orig_op0)
11409 || null_pointer_constant_p (orig_op1))
11410 warning_at (location, OPT_Wextra,
11411 "ordered comparison of pointer with null pointer");
11412
11413 }
11414 else if (!addr_space_superset (as0, as1, &as_common))
11415 {
11416 error_at (location, "comparison of pointers to "
11417 "disjoint address spaces");
11418 return error_mark_node;
11419 }
11420 else
11421 {
11422 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11423 result_type = build_pointer_type
11424 (build_qualified_type (void_type_node, qual));
11425 pedwarn (location, 0,
11426 "comparison of distinct pointer types lacks a cast");
11427 }
11428 }
11429 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11430 {
11431 result_type = type0;
11432 if (pedantic)
11433 pedwarn (location, OPT_Wpedantic,
11434 "ordered comparison of pointer with integer zero");
11435 else if (extra_warnings)
11436 warning_at (location, OPT_Wextra,
11437 "ordered comparison of pointer with integer zero");
11438 }
11439 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11440 {
11441 result_type = type1;
11442 if (pedantic)
11443 pedwarn (location, OPT_Wpedantic,
11444 "ordered comparison of pointer with integer zero");
11445 else if (extra_warnings)
11446 warning_at (location, OPT_Wextra,
11447 "ordered comparison of pointer with integer zero");
11448 }
11449 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11450 {
11451 result_type = type0;
11452 pedwarn (location, 0, "comparison between pointer and integer");
11453 }
11454 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11455 {
11456 result_type = type1;
11457 pedwarn (location, 0, "comparison between pointer and integer");
11458 }
11459 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11460 || truth_value_p (TREE_CODE (orig_op0)))
11461 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11462 || truth_value_p (TREE_CODE (orig_op1))))
11463 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11464 break;
11465
11466 default:
11467 gcc_unreachable ();
11468 }
11469
11470 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11471 return error_mark_node;
11472
11473 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11474 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11475 || !vector_types_compatible_elements_p (type0, type1)))
11476 {
11477 gcc_rich_location richloc (location);
11478 richloc.maybe_add_expr (orig_op0);
11479 richloc.maybe_add_expr (orig_op1);
11480 binary_op_error (&richloc, code, type0, type1);
11481 return error_mark_node;
11482 }
11483
11484 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11485 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11486 &&
11487 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11488 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11489 {
11490 bool first_complex = (code0 == COMPLEX_TYPE);
11491 bool second_complex = (code1 == COMPLEX_TYPE);
11492 int none_complex = (!first_complex && !second_complex);
11493
11494 if (shorten || common || short_compare)
11495 {
11496 result_type = c_common_type (type0, type1);
11497 do_warn_double_promotion (result_type, type0, type1,
11498 "implicit conversion from %qT to %qT "
11499 "to match other operand of binary "
11500 "expression",
11501 location);
11502 if (result_type == error_mark_node)
11503 return error_mark_node;
11504 }
11505
11506 if (first_complex != second_complex
11507 && (code == PLUS_EXPR
11508 || code == MINUS_EXPR
11509 || code == MULT_EXPR
11510 || (code == TRUNC_DIV_EXPR && first_complex))
11511 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11512 && flag_signed_zeros)
11513 {
11514 /* An operation on mixed real/complex operands must be
11515 handled specially, but the language-independent code can
11516 more easily optimize the plain complex arithmetic if
11517 -fno-signed-zeros. */
11518 tree real_type = TREE_TYPE (result_type);
11519 tree real, imag;
11520 if (type0 != orig_type0 || type1 != orig_type1)
11521 {
11522 gcc_assert (may_need_excess_precision && common);
11523 semantic_result_type = c_common_type (orig_type0, orig_type1);
11524 }
11525 if (first_complex)
11526 {
11527 if (TREE_TYPE (op0) != result_type)
11528 op0 = convert_and_check (location, result_type, op0);
11529 if (TREE_TYPE (op1) != real_type)
11530 op1 = convert_and_check (location, real_type, op1);
11531 }
11532 else
11533 {
11534 if (TREE_TYPE (op0) != real_type)
11535 op0 = convert_and_check (location, real_type, op0);
11536 if (TREE_TYPE (op1) != result_type)
11537 op1 = convert_and_check (location, result_type, op1);
11538 }
11539 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11540 return error_mark_node;
11541 if (first_complex)
11542 {
11543 op0 = c_save_expr (op0);
11544 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11545 op0, true);
11546 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11547 op0, true);
11548 switch (code)
11549 {
11550 case MULT_EXPR:
11551 case TRUNC_DIV_EXPR:
11552 op1 = c_save_expr (op1);
11553 imag = build2 (resultcode, real_type, imag, op1);
11554 /* Fall through. */
11555 case PLUS_EXPR:
11556 case MINUS_EXPR:
11557 real = build2 (resultcode, real_type, real, op1);
11558 break;
11559 default:
11560 gcc_unreachable();
11561 }
11562 }
11563 else
11564 {
11565 op1 = c_save_expr (op1);
11566 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11567 op1, true);
11568 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11569 op1, true);
11570 switch (code)
11571 {
11572 case MULT_EXPR:
11573 op0 = c_save_expr (op0);
11574 imag = build2 (resultcode, real_type, op0, imag);
11575 /* Fall through. */
11576 case PLUS_EXPR:
11577 real = build2 (resultcode, real_type, op0, real);
11578 break;
11579 case MINUS_EXPR:
11580 real = build2 (resultcode, real_type, op0, real);
11581 imag = build1 (NEGATE_EXPR, real_type, imag);
11582 break;
11583 default:
11584 gcc_unreachable();
11585 }
11586 }
11587 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11588 goto return_build_binary_op;
11589 }
11590
11591 /* For certain operations (which identify themselves by shorten != 0)
11592 if both args were extended from the same smaller type,
11593 do the arithmetic in that type and then extend.
11594
11595 shorten !=0 and !=1 indicates a bitwise operation.
11596 For them, this optimization is safe only if
11597 both args are zero-extended or both are sign-extended.
11598 Otherwise, we might change the result.
11599 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11600 but calculated in (unsigned short) it would be (unsigned short)-1. */
11601
11602 if (shorten && none_complex)
11603 {
11604 final_type = result_type;
11605 result_type = shorten_binary_op (result_type, op0, op1,
11606 shorten == -1);
11607 }
11608
11609 /* Shifts can be shortened if shifting right. */
11610
11611 if (short_shift)
11612 {
11613 int unsigned_arg;
11614 tree arg0 = get_narrower (op0, &unsigned_arg);
11615
11616 final_type = result_type;
11617
11618 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11619 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11620
11621 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11622 && tree_int_cst_sgn (op1) > 0
11623 /* We can shorten only if the shift count is less than the
11624 number of bits in the smaller type size. */
11625 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11626 /* We cannot drop an unsigned shift after sign-extension. */
11627 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11628 {
11629 /* Do an unsigned shift if the operand was zero-extended. */
11630 result_type
11631 = c_common_signed_or_unsigned_type (unsigned_arg,
11632 TREE_TYPE (arg0));
11633 /* Convert value-to-be-shifted to that type. */
11634 if (TREE_TYPE (op0) != result_type)
11635 op0 = convert (result_type, op0);
11636 converted = 1;
11637 }
11638 }
11639
11640 /* Comparison operations are shortened too but differently.
11641 They identify themselves by setting short_compare = 1. */
11642
11643 if (short_compare)
11644 {
11645 /* Don't write &op0, etc., because that would prevent op0
11646 from being kept in a register.
11647 Instead, make copies of the our local variables and
11648 pass the copies by reference, then copy them back afterward. */
11649 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11650 enum tree_code xresultcode = resultcode;
11651 tree val
11652 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11653 &xresultcode);
11654
11655 if (val != 0)
11656 {
11657 ret = val;
11658 goto return_build_binary_op;
11659 }
11660
11661 op0 = xop0, op1 = xop1;
11662 converted = 1;
11663 resultcode = xresultcode;
11664
11665 if (c_inhibit_evaluation_warnings == 0)
11666 {
11667 bool op0_maybe_const = true;
11668 bool op1_maybe_const = true;
11669 tree orig_op0_folded, orig_op1_folded;
11670
11671 if (in_late_binary_op)
11672 {
11673 orig_op0_folded = orig_op0;
11674 orig_op1_folded = orig_op1;
11675 }
11676 else
11677 {
11678 /* Fold for the sake of possible warnings, as in
11679 build_conditional_expr. This requires the
11680 "original" values to be folded, not just op0 and
11681 op1. */
11682 c_inhibit_evaluation_warnings++;
11683 op0 = c_fully_fold (op0, require_constant_value,
11684 &op0_maybe_const);
11685 op1 = c_fully_fold (op1, require_constant_value,
11686 &op1_maybe_const);
11687 c_inhibit_evaluation_warnings--;
11688 orig_op0_folded = c_fully_fold (orig_op0,
11689 require_constant_value,
11690 NULL);
11691 orig_op1_folded = c_fully_fold (orig_op1,
11692 require_constant_value,
11693 NULL);
11694 }
11695
11696 if (warn_sign_compare)
11697 warn_for_sign_compare (location, orig_op0_folded,
11698 orig_op1_folded, op0, op1,
11699 result_type, resultcode);
11700 if (!in_late_binary_op && !int_operands)
11701 {
11702 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11703 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11704 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11705 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11706 }
11707 }
11708 }
11709 }
11710
11711 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11712 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11713 Then the expression will be built.
11714 It will be given type FINAL_TYPE if that is nonzero;
11715 otherwise, it will be given type RESULT_TYPE. */
11716
11717 if (!result_type)
11718 {
11719 gcc_rich_location richloc (location);
11720 richloc.maybe_add_expr (orig_op0);
11721 richloc.maybe_add_expr (orig_op1);
11722 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
11723 return error_mark_node;
11724 }
11725
11726 if (build_type == NULL_TREE)
11727 {
11728 build_type = result_type;
11729 if ((type0 != orig_type0 || type1 != orig_type1)
11730 && !boolean_op)
11731 {
11732 gcc_assert (may_need_excess_precision && common);
11733 semantic_result_type = c_common_type (orig_type0, orig_type1);
11734 }
11735 }
11736
11737 if (!converted)
11738 {
11739 op0 = ep_convert_and_check (location, result_type, op0,
11740 semantic_result_type);
11741 op1 = ep_convert_and_check (location, result_type, op1,
11742 semantic_result_type);
11743
11744 /* This can happen if one operand has a vector type, and the other
11745 has a different type. */
11746 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11747 return error_mark_node;
11748 }
11749
11750 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11751 | SANITIZE_FLOAT_DIVIDE))
11752 && do_ubsan_in_current_function ()
11753 && (doing_div_or_mod || doing_shift)
11754 && !require_constant_value)
11755 {
11756 /* OP0 and/or OP1 might have side-effects. */
11757 op0 = c_save_expr (op0);
11758 op1 = c_save_expr (op1);
11759 op0 = c_fully_fold (op0, false, NULL);
11760 op1 = c_fully_fold (op1, false, NULL);
11761 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11762 | SANITIZE_FLOAT_DIVIDE)))
11763 instrument_expr = ubsan_instrument_division (location, op0, op1);
11764 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11765 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11766 }
11767
11768 /* Treat expressions in initializers specially as they can't trap. */
11769 if (int_const_or_overflow)
11770 ret = (require_constant_value
11771 ? fold_build2_initializer_loc (location, resultcode, build_type,
11772 op0, op1)
11773 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11774 else
11775 ret = build2 (resultcode, build_type, op0, op1);
11776 if (final_type != 0)
11777 ret = convert (final_type, ret);
11778
11779 return_build_binary_op:
11780 gcc_assert (ret != error_mark_node);
11781 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11782 ret = (int_operands
11783 ? note_integer_operands (ret)
11784 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11785 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11786 && !in_late_binary_op)
11787 ret = note_integer_operands (ret);
11788 if (semantic_result_type)
11789 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11790 protected_set_expr_location (ret, location);
11791
11792 if (instrument_expr != NULL)
11793 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11794 instrument_expr, ret);
11795
11796 return ret;
11797 }
11798
11799
11800 /* Convert EXPR to be a truth-value, validating its type for this
11801 purpose. LOCATION is the source location for the expression. */
11802
11803 tree
11804 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11805 {
11806 bool int_const, int_operands;
11807
11808 switch (TREE_CODE (TREE_TYPE (expr)))
11809 {
11810 case ARRAY_TYPE:
11811 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11812 return error_mark_node;
11813
11814 case RECORD_TYPE:
11815 error_at (location, "used struct type value where scalar is required");
11816 return error_mark_node;
11817
11818 case UNION_TYPE:
11819 error_at (location, "used union type value where scalar is required");
11820 return error_mark_node;
11821
11822 case VOID_TYPE:
11823 error_at (location, "void value not ignored as it ought to be");
11824 return error_mark_node;
11825
11826 case POINTER_TYPE:
11827 if (reject_gcc_builtin (expr))
11828 return error_mark_node;
11829 break;
11830
11831 case FUNCTION_TYPE:
11832 gcc_unreachable ();
11833
11834 case VECTOR_TYPE:
11835 error_at (location, "used vector type where scalar is required");
11836 return error_mark_node;
11837
11838 default:
11839 break;
11840 }
11841
11842 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11843 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11844 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11845 {
11846 expr = remove_c_maybe_const_expr (expr);
11847 expr = build2 (NE_EXPR, integer_type_node, expr,
11848 convert (TREE_TYPE (expr), integer_zero_node));
11849 expr = note_integer_operands (expr);
11850 }
11851 else
11852 /* ??? Should we also give an error for vectors rather than leaving
11853 those to give errors later? */
11854 expr = c_common_truthvalue_conversion (location, expr);
11855
11856 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11857 {
11858 if (TREE_OVERFLOW (expr))
11859 return expr;
11860 else
11861 return note_integer_operands (expr);
11862 }
11863 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11864 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11865 return expr;
11866 }
11867 \f
11868
11869 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11870 required. */
11871
11872 tree
11873 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11874 {
11875 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11876 {
11877 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11878 /* Executing a compound literal inside a function reinitializes
11879 it. */
11880 if (!TREE_STATIC (decl))
11881 *se = true;
11882 return decl;
11883 }
11884 else
11885 return expr;
11886 }
11887 \f
11888 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11889 statement. LOC is the location of the construct. */
11890
11891 tree
11892 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11893 tree clauses)
11894 {
11895 body = c_end_compound_stmt (loc, body, true);
11896
11897 tree stmt = make_node (code);
11898 TREE_TYPE (stmt) = void_type_node;
11899 OMP_BODY (stmt) = body;
11900 OMP_CLAUSES (stmt) = clauses;
11901 SET_EXPR_LOCATION (stmt, loc);
11902
11903 return add_stmt (stmt);
11904 }
11905
11906 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11907 statement. LOC is the location of the OACC_DATA. */
11908
11909 tree
11910 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11911 {
11912 tree stmt;
11913
11914 block = c_end_compound_stmt (loc, block, true);
11915
11916 stmt = make_node (OACC_DATA);
11917 TREE_TYPE (stmt) = void_type_node;
11918 OACC_DATA_CLAUSES (stmt) = clauses;
11919 OACC_DATA_BODY (stmt) = block;
11920 SET_EXPR_LOCATION (stmt, loc);
11921
11922 return add_stmt (stmt);
11923 }
11924
11925 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
11926 statement. LOC is the location of the OACC_HOST_DATA. */
11927
11928 tree
11929 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
11930 {
11931 tree stmt;
11932
11933 block = c_end_compound_stmt (loc, block, true);
11934
11935 stmt = make_node (OACC_HOST_DATA);
11936 TREE_TYPE (stmt) = void_type_node;
11937 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
11938 OACC_HOST_DATA_BODY (stmt) = block;
11939 SET_EXPR_LOCATION (stmt, loc);
11940
11941 return add_stmt (stmt);
11942 }
11943
11944 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11945
11946 tree
11947 c_begin_omp_parallel (void)
11948 {
11949 tree block;
11950
11951 keep_next_level ();
11952 block = c_begin_compound_stmt (true);
11953
11954 return block;
11955 }
11956
11957 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11958 statement. LOC is the location of the OMP_PARALLEL. */
11959
11960 tree
11961 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11962 {
11963 tree stmt;
11964
11965 block = c_end_compound_stmt (loc, block, true);
11966
11967 stmt = make_node (OMP_PARALLEL);
11968 TREE_TYPE (stmt) = void_type_node;
11969 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11970 OMP_PARALLEL_BODY (stmt) = block;
11971 SET_EXPR_LOCATION (stmt, loc);
11972
11973 return add_stmt (stmt);
11974 }
11975
11976 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11977
11978 tree
11979 c_begin_omp_task (void)
11980 {
11981 tree block;
11982
11983 keep_next_level ();
11984 block = c_begin_compound_stmt (true);
11985
11986 return block;
11987 }
11988
11989 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11990 statement. LOC is the location of the #pragma. */
11991
11992 tree
11993 c_finish_omp_task (location_t loc, tree clauses, tree block)
11994 {
11995 tree stmt;
11996
11997 block = c_end_compound_stmt (loc, block, true);
11998
11999 stmt = make_node (OMP_TASK);
12000 TREE_TYPE (stmt) = void_type_node;
12001 OMP_TASK_CLAUSES (stmt) = clauses;
12002 OMP_TASK_BODY (stmt) = block;
12003 SET_EXPR_LOCATION (stmt, loc);
12004
12005 return add_stmt (stmt);
12006 }
12007
12008 /* Generate GOMP_cancel call for #pragma omp cancel. */
12009
12010 void
12011 c_finish_omp_cancel (location_t loc, tree clauses)
12012 {
12013 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12014 int mask = 0;
12015 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
12016 mask = 1;
12017 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
12018 mask = 2;
12019 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
12020 mask = 4;
12021 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
12022 mask = 8;
12023 else
12024 {
12025 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12026 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12027 "clauses");
12028 return;
12029 }
12030 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
12031 if (ifc != NULL_TREE)
12032 {
12033 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12034 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12035 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12036 build_zero_cst (type));
12037 }
12038 else
12039 ifc = boolean_true_node;
12040 tree stmt = build_call_expr_loc (loc, fn, 2,
12041 build_int_cst (integer_type_node, mask),
12042 ifc);
12043 add_stmt (stmt);
12044 }
12045
12046 /* Generate GOMP_cancellation_point call for
12047 #pragma omp cancellation point. */
12048
12049 void
12050 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12051 {
12052 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12053 int mask = 0;
12054 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
12055 mask = 1;
12056 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
12057 mask = 2;
12058 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
12059 mask = 4;
12060 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
12061 mask = 8;
12062 else
12063 {
12064 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12065 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12066 "clauses");
12067 return;
12068 }
12069 tree stmt = build_call_expr_loc (loc, fn, 1,
12070 build_int_cst (integer_type_node, mask));
12071 add_stmt (stmt);
12072 }
12073
12074 /* Helper function for handle_omp_array_sections. Called recursively
12075 to handle multiple array-section-subscripts. C is the clause,
12076 T current expression (initially OMP_CLAUSE_DECL), which is either
12077 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12078 expression if specified, TREE_VALUE length expression if specified,
12079 TREE_CHAIN is what it has been specified after, or some decl.
12080 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12081 set to true if any of the array-section-subscript could have length
12082 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12083 first array-section-subscript which is known not to have length
12084 of one. Given say:
12085 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12086 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12087 all are or may have length of 1, array-section-subscript [:2] is the
12088 first one known not to have length 1. For array-section-subscript
12089 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12090 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12091 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12092 case though, as some lengths could be zero. */
12093
12094 static tree
12095 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12096 bool &maybe_zero_len, unsigned int &first_non_one,
12097 enum c_omp_region_type ort)
12098 {
12099 tree ret, low_bound, length, type;
12100 if (TREE_CODE (t) != TREE_LIST)
12101 {
12102 if (error_operand_p (t))
12103 return error_mark_node;
12104 ret = t;
12105 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12106 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12107 {
12108 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12109 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12110 return error_mark_node;
12111 }
12112 if (TREE_CODE (t) == COMPONENT_REF
12113 && ort == C_ORT_OMP
12114 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12115 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12116 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12117 {
12118 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12119 {
12120 error_at (OMP_CLAUSE_LOCATION (c),
12121 "bit-field %qE in %qs clause",
12122 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12123 return error_mark_node;
12124 }
12125 while (TREE_CODE (t) == COMPONENT_REF)
12126 {
12127 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12128 {
12129 error_at (OMP_CLAUSE_LOCATION (c),
12130 "%qE is a member of a union", t);
12131 return error_mark_node;
12132 }
12133 t = TREE_OPERAND (t, 0);
12134 }
12135 }
12136 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12137 {
12138 if (DECL_P (t))
12139 error_at (OMP_CLAUSE_LOCATION (c),
12140 "%qD is not a variable in %qs clause", t,
12141 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12142 else
12143 error_at (OMP_CLAUSE_LOCATION (c),
12144 "%qE is not a variable in %qs clause", t,
12145 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12146 return error_mark_node;
12147 }
12148 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12149 && TYPE_ATOMIC (TREE_TYPE (t)))
12150 {
12151 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12152 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12153 return error_mark_node;
12154 }
12155 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12156 && VAR_P (t)
12157 && DECL_THREAD_LOCAL_P (t))
12158 {
12159 error_at (OMP_CLAUSE_LOCATION (c),
12160 "%qD is threadprivate variable in %qs clause", t,
12161 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12162 return error_mark_node;
12163 }
12164 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12165 && TYPE_ATOMIC (TREE_TYPE (t))
12166 && POINTER_TYPE_P (TREE_TYPE (t)))
12167 {
12168 /* If the array section is pointer based and the pointer
12169 itself is _Atomic qualified, we need to atomically load
12170 the pointer. */
12171 c_expr expr;
12172 memset (&expr, 0, sizeof (expr));
12173 expr.value = ret;
12174 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12175 expr, false, false);
12176 ret = expr.value;
12177 }
12178 return ret;
12179 }
12180
12181 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12182 maybe_zero_len, first_non_one, ort);
12183 if (ret == error_mark_node || ret == NULL_TREE)
12184 return ret;
12185
12186 type = TREE_TYPE (ret);
12187 low_bound = TREE_PURPOSE (t);
12188 length = TREE_VALUE (t);
12189
12190 if (low_bound == error_mark_node || length == error_mark_node)
12191 return error_mark_node;
12192
12193 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12194 {
12195 error_at (OMP_CLAUSE_LOCATION (c),
12196 "low bound %qE of array section does not have integral type",
12197 low_bound);
12198 return error_mark_node;
12199 }
12200 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12201 {
12202 error_at (OMP_CLAUSE_LOCATION (c),
12203 "length %qE of array section does not have integral type",
12204 length);
12205 return error_mark_node;
12206 }
12207 if (low_bound
12208 && TREE_CODE (low_bound) == INTEGER_CST
12209 && TYPE_PRECISION (TREE_TYPE (low_bound))
12210 > TYPE_PRECISION (sizetype))
12211 low_bound = fold_convert (sizetype, low_bound);
12212 if (length
12213 && TREE_CODE (length) == INTEGER_CST
12214 && TYPE_PRECISION (TREE_TYPE (length))
12215 > TYPE_PRECISION (sizetype))
12216 length = fold_convert (sizetype, length);
12217 if (low_bound == NULL_TREE)
12218 low_bound = integer_zero_node;
12219
12220 if (length != NULL_TREE)
12221 {
12222 if (!integer_nonzerop (length))
12223 {
12224 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12225 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12226 {
12227 if (integer_zerop (length))
12228 {
12229 error_at (OMP_CLAUSE_LOCATION (c),
12230 "zero length array section in %qs clause",
12231 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12232 return error_mark_node;
12233 }
12234 }
12235 else
12236 maybe_zero_len = true;
12237 }
12238 if (first_non_one == types.length ()
12239 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12240 first_non_one++;
12241 }
12242 if (TREE_CODE (type) == ARRAY_TYPE)
12243 {
12244 if (length == NULL_TREE
12245 && (TYPE_DOMAIN (type) == NULL_TREE
12246 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12247 {
12248 error_at (OMP_CLAUSE_LOCATION (c),
12249 "for unknown bound array type length expression must "
12250 "be specified");
12251 return error_mark_node;
12252 }
12253 if (TREE_CODE (low_bound) == INTEGER_CST
12254 && tree_int_cst_sgn (low_bound) == -1)
12255 {
12256 error_at (OMP_CLAUSE_LOCATION (c),
12257 "negative low bound in array section in %qs clause",
12258 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12259 return error_mark_node;
12260 }
12261 if (length != NULL_TREE
12262 && TREE_CODE (length) == INTEGER_CST
12263 && tree_int_cst_sgn (length) == -1)
12264 {
12265 error_at (OMP_CLAUSE_LOCATION (c),
12266 "negative length in array section in %qs clause",
12267 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12268 return error_mark_node;
12269 }
12270 if (TYPE_DOMAIN (type)
12271 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12272 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12273 == INTEGER_CST)
12274 {
12275 tree size = size_binop (PLUS_EXPR,
12276 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12277 size_one_node);
12278 if (TREE_CODE (low_bound) == INTEGER_CST)
12279 {
12280 if (tree_int_cst_lt (size, low_bound))
12281 {
12282 error_at (OMP_CLAUSE_LOCATION (c),
12283 "low bound %qE above array section size "
12284 "in %qs clause", low_bound,
12285 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12286 return error_mark_node;
12287 }
12288 if (tree_int_cst_equal (size, low_bound))
12289 {
12290 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12291 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12292 {
12293 error_at (OMP_CLAUSE_LOCATION (c),
12294 "zero length array section in %qs clause",
12295 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12296 return error_mark_node;
12297 }
12298 maybe_zero_len = true;
12299 }
12300 else if (length == NULL_TREE
12301 && first_non_one == types.length ()
12302 && tree_int_cst_equal
12303 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12304 low_bound))
12305 first_non_one++;
12306 }
12307 else if (length == NULL_TREE)
12308 {
12309 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12310 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12311 maybe_zero_len = true;
12312 if (first_non_one == types.length ())
12313 first_non_one++;
12314 }
12315 if (length && TREE_CODE (length) == INTEGER_CST)
12316 {
12317 if (tree_int_cst_lt (size, length))
12318 {
12319 error_at (OMP_CLAUSE_LOCATION (c),
12320 "length %qE above array section size "
12321 "in %qs clause", length,
12322 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12323 return error_mark_node;
12324 }
12325 if (TREE_CODE (low_bound) == INTEGER_CST)
12326 {
12327 tree lbpluslen
12328 = size_binop (PLUS_EXPR,
12329 fold_convert (sizetype, low_bound),
12330 fold_convert (sizetype, length));
12331 if (TREE_CODE (lbpluslen) == INTEGER_CST
12332 && tree_int_cst_lt (size, lbpluslen))
12333 {
12334 error_at (OMP_CLAUSE_LOCATION (c),
12335 "high bound %qE above array section size "
12336 "in %qs clause", lbpluslen,
12337 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12338 return error_mark_node;
12339 }
12340 }
12341 }
12342 }
12343 else if (length == NULL_TREE)
12344 {
12345 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12346 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12347 maybe_zero_len = true;
12348 if (first_non_one == types.length ())
12349 first_non_one++;
12350 }
12351
12352 /* For [lb:] we will need to evaluate lb more than once. */
12353 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12354 {
12355 tree lb = c_save_expr (low_bound);
12356 if (lb != low_bound)
12357 {
12358 TREE_PURPOSE (t) = lb;
12359 low_bound = lb;
12360 }
12361 }
12362 }
12363 else if (TREE_CODE (type) == POINTER_TYPE)
12364 {
12365 if (length == NULL_TREE)
12366 {
12367 error_at (OMP_CLAUSE_LOCATION (c),
12368 "for pointer type length expression must be specified");
12369 return error_mark_node;
12370 }
12371 if (length != NULL_TREE
12372 && TREE_CODE (length) == INTEGER_CST
12373 && tree_int_cst_sgn (length) == -1)
12374 {
12375 error_at (OMP_CLAUSE_LOCATION (c),
12376 "negative length in array section in %qs clause",
12377 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12378 return error_mark_node;
12379 }
12380 /* If there is a pointer type anywhere but in the very first
12381 array-section-subscript, the array section can't be contiguous. */
12382 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12383 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12384 {
12385 error_at (OMP_CLAUSE_LOCATION (c),
12386 "array section is not contiguous in %qs clause",
12387 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12388 return error_mark_node;
12389 }
12390 }
12391 else
12392 {
12393 error_at (OMP_CLAUSE_LOCATION (c),
12394 "%qE does not have pointer or array type", ret);
12395 return error_mark_node;
12396 }
12397 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12398 types.safe_push (TREE_TYPE (ret));
12399 /* We will need to evaluate lb more than once. */
12400 tree lb = c_save_expr (low_bound);
12401 if (lb != low_bound)
12402 {
12403 TREE_PURPOSE (t) = lb;
12404 low_bound = lb;
12405 }
12406 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12407 return ret;
12408 }
12409
12410 /* Handle array sections for clause C. */
12411
12412 static bool
12413 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12414 {
12415 bool maybe_zero_len = false;
12416 unsigned int first_non_one = 0;
12417 auto_vec<tree, 10> types;
12418 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12419 maybe_zero_len, first_non_one,
12420 ort);
12421 if (first == error_mark_node)
12422 return true;
12423 if (first == NULL_TREE)
12424 return false;
12425 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12426 {
12427 tree t = OMP_CLAUSE_DECL (c);
12428 tree tem = NULL_TREE;
12429 /* Need to evaluate side effects in the length expressions
12430 if any. */
12431 while (TREE_CODE (t) == TREE_LIST)
12432 {
12433 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12434 {
12435 if (tem == NULL_TREE)
12436 tem = TREE_VALUE (t);
12437 else
12438 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12439 TREE_VALUE (t), tem);
12440 }
12441 t = TREE_CHAIN (t);
12442 }
12443 if (tem)
12444 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12445 first = c_fully_fold (first, false, NULL);
12446 OMP_CLAUSE_DECL (c) = first;
12447 }
12448 else
12449 {
12450 unsigned int num = types.length (), i;
12451 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12452 tree condition = NULL_TREE;
12453
12454 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12455 maybe_zero_len = true;
12456
12457 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12458 t = TREE_CHAIN (t))
12459 {
12460 tree low_bound = TREE_PURPOSE (t);
12461 tree length = TREE_VALUE (t);
12462
12463 i--;
12464 if (low_bound
12465 && TREE_CODE (low_bound) == INTEGER_CST
12466 && TYPE_PRECISION (TREE_TYPE (low_bound))
12467 > TYPE_PRECISION (sizetype))
12468 low_bound = fold_convert (sizetype, low_bound);
12469 if (length
12470 && TREE_CODE (length) == INTEGER_CST
12471 && TYPE_PRECISION (TREE_TYPE (length))
12472 > TYPE_PRECISION (sizetype))
12473 length = fold_convert (sizetype, length);
12474 if (low_bound == NULL_TREE)
12475 low_bound = integer_zero_node;
12476 if (!maybe_zero_len && i > first_non_one)
12477 {
12478 if (integer_nonzerop (low_bound))
12479 goto do_warn_noncontiguous;
12480 if (length != NULL_TREE
12481 && TREE_CODE (length) == INTEGER_CST
12482 && TYPE_DOMAIN (types[i])
12483 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12484 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12485 == INTEGER_CST)
12486 {
12487 tree size;
12488 size = size_binop (PLUS_EXPR,
12489 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12490 size_one_node);
12491 if (!tree_int_cst_equal (length, size))
12492 {
12493 do_warn_noncontiguous:
12494 error_at (OMP_CLAUSE_LOCATION (c),
12495 "array section is not contiguous in %qs "
12496 "clause",
12497 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12498 return true;
12499 }
12500 }
12501 if (length != NULL_TREE
12502 && TREE_SIDE_EFFECTS (length))
12503 {
12504 if (side_effects == NULL_TREE)
12505 side_effects = length;
12506 else
12507 side_effects = build2 (COMPOUND_EXPR,
12508 TREE_TYPE (side_effects),
12509 length, side_effects);
12510 }
12511 }
12512 else
12513 {
12514 tree l;
12515
12516 if (i > first_non_one
12517 && ((length && integer_nonzerop (length))
12518 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12519 continue;
12520 if (length)
12521 l = fold_convert (sizetype, length);
12522 else
12523 {
12524 l = size_binop (PLUS_EXPR,
12525 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12526 size_one_node);
12527 l = size_binop (MINUS_EXPR, l,
12528 fold_convert (sizetype, low_bound));
12529 }
12530 if (i > first_non_one)
12531 {
12532 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12533 size_zero_node);
12534 if (condition == NULL_TREE)
12535 condition = l;
12536 else
12537 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12538 l, condition);
12539 }
12540 else if (size == NULL_TREE)
12541 {
12542 size = size_in_bytes (TREE_TYPE (types[i]));
12543 tree eltype = TREE_TYPE (types[num - 1]);
12544 while (TREE_CODE (eltype) == ARRAY_TYPE)
12545 eltype = TREE_TYPE (eltype);
12546 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12547 {
12548 if (integer_zerop (size)
12549 || integer_zerop (size_in_bytes (eltype)))
12550 {
12551 error_at (OMP_CLAUSE_LOCATION (c),
12552 "zero length array section in %qs clause",
12553 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12554 return error_mark_node;
12555 }
12556 size = size_binop (EXACT_DIV_EXPR, size,
12557 size_in_bytes (eltype));
12558 }
12559 size = size_binop (MULT_EXPR, size, l);
12560 if (condition)
12561 size = fold_build3 (COND_EXPR, sizetype, condition,
12562 size, size_zero_node);
12563 }
12564 else
12565 size = size_binop (MULT_EXPR, size, l);
12566 }
12567 }
12568 if (side_effects)
12569 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12570 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12571 {
12572 size = size_binop (MINUS_EXPR, size, size_one_node);
12573 size = c_fully_fold (size, false, NULL);
12574 tree index_type = build_index_type (size);
12575 tree eltype = TREE_TYPE (first);
12576 while (TREE_CODE (eltype) == ARRAY_TYPE)
12577 eltype = TREE_TYPE (eltype);
12578 tree type = build_array_type (eltype, index_type);
12579 tree ptype = build_pointer_type (eltype);
12580 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12581 t = build_fold_addr_expr (t);
12582 tree t2 = build_fold_addr_expr (first);
12583 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12584 ptrdiff_type_node, t2);
12585 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12586 ptrdiff_type_node, t2,
12587 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12588 ptrdiff_type_node, t));
12589 t2 = c_fully_fold (t2, false, NULL);
12590 if (tree_fits_shwi_p (t2))
12591 t = build2 (MEM_REF, type, t,
12592 build_int_cst (ptype, tree_to_shwi (t2)));
12593 else
12594 {
12595 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12596 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12597 TREE_TYPE (t), t, t2);
12598 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12599 }
12600 OMP_CLAUSE_DECL (c) = t;
12601 return false;
12602 }
12603 first = c_fully_fold (first, false, NULL);
12604 OMP_CLAUSE_DECL (c) = first;
12605 if (size)
12606 size = c_fully_fold (size, false, NULL);
12607 OMP_CLAUSE_SIZE (c) = size;
12608 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12609 || (TREE_CODE (t) == COMPONENT_REF
12610 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12611 return false;
12612 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12613 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
12614 switch (OMP_CLAUSE_MAP_KIND (c))
12615 {
12616 case GOMP_MAP_ALLOC:
12617 case GOMP_MAP_TO:
12618 case GOMP_MAP_FROM:
12619 case GOMP_MAP_TOFROM:
12620 case GOMP_MAP_ALWAYS_TO:
12621 case GOMP_MAP_ALWAYS_FROM:
12622 case GOMP_MAP_ALWAYS_TOFROM:
12623 case GOMP_MAP_RELEASE:
12624 case GOMP_MAP_DELETE:
12625 case GOMP_MAP_FORCE_TO:
12626 case GOMP_MAP_FORCE_FROM:
12627 case GOMP_MAP_FORCE_TOFROM:
12628 case GOMP_MAP_FORCE_PRESENT:
12629 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12630 break;
12631 default:
12632 break;
12633 }
12634 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12635 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
12636 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12637 else if (TREE_CODE (t) == COMPONENT_REF)
12638 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12639 else
12640 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12641 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12642 && !c_mark_addressable (t))
12643 return false;
12644 OMP_CLAUSE_DECL (c2) = t;
12645 t = build_fold_addr_expr (first);
12646 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12647 tree ptr = OMP_CLAUSE_DECL (c2);
12648 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12649 ptr = build_fold_addr_expr (ptr);
12650 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12651 ptrdiff_type_node, t,
12652 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12653 ptrdiff_type_node, ptr));
12654 t = c_fully_fold (t, false, NULL);
12655 OMP_CLAUSE_SIZE (c2) = t;
12656 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12657 OMP_CLAUSE_CHAIN (c) = c2;
12658 }
12659 return false;
12660 }
12661
12662 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12663 an inline call. But, remap
12664 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12665 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12666
12667 static tree
12668 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12669 tree decl, tree placeholder)
12670 {
12671 copy_body_data id;
12672 hash_map<tree, tree> decl_map;
12673
12674 decl_map.put (omp_decl1, placeholder);
12675 decl_map.put (omp_decl2, decl);
12676 memset (&id, 0, sizeof (id));
12677 id.src_fn = DECL_CONTEXT (omp_decl1);
12678 id.dst_fn = current_function_decl;
12679 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12680 id.decl_map = &decl_map;
12681
12682 id.copy_decl = copy_decl_no_change;
12683 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12684 id.transform_new_cfg = true;
12685 id.transform_return_to_modify = false;
12686 id.transform_lang_insert_block = NULL;
12687 id.eh_lp_nr = 0;
12688 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12689 return stmt;
12690 }
12691
12692 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12693 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12694
12695 static tree
12696 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12697 {
12698 if (*tp == (tree) data)
12699 return *tp;
12700 return NULL_TREE;
12701 }
12702
12703 /* For all elements of CLAUSES, validate them against their constraints.
12704 Remove any elements from the list that are invalid. */
12705
12706 tree
12707 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
12708 {
12709 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12710 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
12711 tree c, t, type, *pc;
12712 tree simdlen = NULL_TREE, safelen = NULL_TREE;
12713 bool branch_seen = false;
12714 bool copyprivate_seen = false;
12715 bool linear_variable_step_check = false;
12716 tree *nowait_clause = NULL;
12717 bool ordered_seen = false;
12718 tree schedule_clause = NULL_TREE;
12719 bool oacc_async = false;
12720
12721 bitmap_obstack_initialize (NULL);
12722 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12723 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12724 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12725 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12726 bitmap_initialize (&map_head, &bitmap_default_obstack);
12727 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
12728 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
12729
12730 if (ort & C_ORT_ACC)
12731 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12732 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12733 {
12734 oacc_async = true;
12735 break;
12736 }
12737
12738 for (pc = &clauses, c = clauses; c ; c = *pc)
12739 {
12740 bool remove = false;
12741 bool need_complete = false;
12742 bool need_implicitly_determined = false;
12743
12744 switch (OMP_CLAUSE_CODE (c))
12745 {
12746 case OMP_CLAUSE_SHARED:
12747 need_implicitly_determined = true;
12748 goto check_dup_generic;
12749
12750 case OMP_CLAUSE_PRIVATE:
12751 need_complete = true;
12752 need_implicitly_determined = true;
12753 goto check_dup_generic;
12754
12755 case OMP_CLAUSE_REDUCTION:
12756 need_implicitly_determined = true;
12757 t = OMP_CLAUSE_DECL (c);
12758 if (TREE_CODE (t) == TREE_LIST)
12759 {
12760 if (handle_omp_array_sections (c, ort))
12761 {
12762 remove = true;
12763 break;
12764 }
12765
12766 t = OMP_CLAUSE_DECL (c);
12767 }
12768 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
12769 if (t == error_mark_node)
12770 {
12771 remove = true;
12772 break;
12773 }
12774 if (oacc_async)
12775 c_mark_addressable (t);
12776 type = TREE_TYPE (t);
12777 if (TREE_CODE (t) == MEM_REF)
12778 type = TREE_TYPE (type);
12779 if (TREE_CODE (type) == ARRAY_TYPE)
12780 {
12781 tree oatype = type;
12782 gcc_assert (TREE_CODE (t) != MEM_REF);
12783 while (TREE_CODE (type) == ARRAY_TYPE)
12784 type = TREE_TYPE (type);
12785 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12786 {
12787 error_at (OMP_CLAUSE_LOCATION (c),
12788 "%qD in %<reduction%> clause is a zero size array",
12789 t);
12790 remove = true;
12791 break;
12792 }
12793 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12794 TYPE_SIZE_UNIT (type));
12795 if (integer_zerop (size))
12796 {
12797 error_at (OMP_CLAUSE_LOCATION (c),
12798 "%qD in %<reduction%> clause is a zero size array",
12799 t);
12800 remove = true;
12801 break;
12802 }
12803 size = size_binop (MINUS_EXPR, size, size_one_node);
12804 tree index_type = build_index_type (size);
12805 tree atype = build_array_type (type, index_type);
12806 tree ptype = build_pointer_type (type);
12807 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12808 t = build_fold_addr_expr (t);
12809 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12810 OMP_CLAUSE_DECL (c) = t;
12811 }
12812 if (TYPE_ATOMIC (type))
12813 {
12814 error_at (OMP_CLAUSE_LOCATION (c),
12815 "%<_Atomic%> %qE in %<reduction%> clause", t);
12816 remove = true;
12817 break;
12818 }
12819 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12820 && (FLOAT_TYPE_P (type)
12821 || TREE_CODE (type) == COMPLEX_TYPE))
12822 {
12823 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12824 const char *r_name = NULL;
12825
12826 switch (r_code)
12827 {
12828 case PLUS_EXPR:
12829 case MULT_EXPR:
12830 case MINUS_EXPR:
12831 break;
12832 case MIN_EXPR:
12833 if (TREE_CODE (type) == COMPLEX_TYPE)
12834 r_name = "min";
12835 break;
12836 case MAX_EXPR:
12837 if (TREE_CODE (type) == COMPLEX_TYPE)
12838 r_name = "max";
12839 break;
12840 case BIT_AND_EXPR:
12841 r_name = "&";
12842 break;
12843 case BIT_XOR_EXPR:
12844 r_name = "^";
12845 break;
12846 case BIT_IOR_EXPR:
12847 r_name = "|";
12848 break;
12849 case TRUTH_ANDIF_EXPR:
12850 if (FLOAT_TYPE_P (type))
12851 r_name = "&&";
12852 break;
12853 case TRUTH_ORIF_EXPR:
12854 if (FLOAT_TYPE_P (type))
12855 r_name = "||";
12856 break;
12857 default:
12858 gcc_unreachable ();
12859 }
12860 if (r_name)
12861 {
12862 error_at (OMP_CLAUSE_LOCATION (c),
12863 "%qE has invalid type for %<reduction(%s)%>",
12864 t, r_name);
12865 remove = true;
12866 break;
12867 }
12868 }
12869 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12870 {
12871 error_at (OMP_CLAUSE_LOCATION (c),
12872 "user defined reduction not found for %qE", t);
12873 remove = true;
12874 break;
12875 }
12876 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12877 {
12878 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12879 type = TYPE_MAIN_VARIANT (type);
12880 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12881 VAR_DECL, NULL_TREE, type);
12882 tree decl_placeholder = NULL_TREE;
12883 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12884 DECL_ARTIFICIAL (placeholder) = 1;
12885 DECL_IGNORED_P (placeholder) = 1;
12886 if (TREE_CODE (t) == MEM_REF)
12887 {
12888 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12889 VAR_DECL, NULL_TREE, type);
12890 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12891 DECL_ARTIFICIAL (decl_placeholder) = 1;
12892 DECL_IGNORED_P (decl_placeholder) = 1;
12893 }
12894 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12895 c_mark_addressable (placeholder);
12896 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12897 c_mark_addressable (decl_placeholder ? decl_placeholder
12898 : OMP_CLAUSE_DECL (c));
12899 OMP_CLAUSE_REDUCTION_MERGE (c)
12900 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12901 TREE_VEC_ELT (list, 0),
12902 TREE_VEC_ELT (list, 1),
12903 decl_placeholder ? decl_placeholder
12904 : OMP_CLAUSE_DECL (c), placeholder);
12905 OMP_CLAUSE_REDUCTION_MERGE (c)
12906 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12907 void_type_node, NULL_TREE,
12908 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12909 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12910 if (TREE_VEC_LENGTH (list) == 6)
12911 {
12912 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12913 c_mark_addressable (decl_placeholder ? decl_placeholder
12914 : OMP_CLAUSE_DECL (c));
12915 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12916 c_mark_addressable (placeholder);
12917 tree init = TREE_VEC_ELT (list, 5);
12918 if (init == error_mark_node)
12919 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12920 OMP_CLAUSE_REDUCTION_INIT (c)
12921 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12922 TREE_VEC_ELT (list, 3),
12923 decl_placeholder ? decl_placeholder
12924 : OMP_CLAUSE_DECL (c), placeholder);
12925 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12926 {
12927 tree v = decl_placeholder ? decl_placeholder : t;
12928 OMP_CLAUSE_REDUCTION_INIT (c)
12929 = build2 (INIT_EXPR, TREE_TYPE (v), v,
12930 OMP_CLAUSE_REDUCTION_INIT (c));
12931 }
12932 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12933 c_find_omp_placeholder_r,
12934 placeholder, NULL))
12935 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12936 }
12937 else
12938 {
12939 tree init;
12940 tree v = decl_placeholder ? decl_placeholder : t;
12941 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
12942 init = build_constructor (TREE_TYPE (v), NULL);
12943 else
12944 init = fold_convert (TREE_TYPE (v), integer_zero_node);
12945 OMP_CLAUSE_REDUCTION_INIT (c)
12946 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
12947 }
12948 OMP_CLAUSE_REDUCTION_INIT (c)
12949 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12950 void_type_node, NULL_TREE,
12951 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12952 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12953 }
12954 if (TREE_CODE (t) == MEM_REF)
12955 {
12956 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
12957 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
12958 != INTEGER_CST)
12959 {
12960 sorry ("variable length element type in array "
12961 "%<reduction%> clause");
12962 remove = true;
12963 break;
12964 }
12965 t = TREE_OPERAND (t, 0);
12966 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
12967 t = TREE_OPERAND (t, 0);
12968 if (TREE_CODE (t) == ADDR_EXPR)
12969 t = TREE_OPERAND (t, 0);
12970 }
12971 goto check_dup_generic_t;
12972
12973 case OMP_CLAUSE_COPYPRIVATE:
12974 copyprivate_seen = true;
12975 if (nowait_clause)
12976 {
12977 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12978 "%<nowait%> clause must not be used together "
12979 "with %<copyprivate%>");
12980 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12981 nowait_clause = NULL;
12982 }
12983 goto check_dup_generic;
12984
12985 case OMP_CLAUSE_COPYIN:
12986 t = OMP_CLAUSE_DECL (c);
12987 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
12988 {
12989 error_at (OMP_CLAUSE_LOCATION (c),
12990 "%qE must be %<threadprivate%> for %<copyin%>", t);
12991 remove = true;
12992 break;
12993 }
12994 goto check_dup_generic;
12995
12996 case OMP_CLAUSE_LINEAR:
12997 if (ort != C_ORT_OMP_DECLARE_SIMD)
12998 need_implicitly_determined = true;
12999 t = OMP_CLAUSE_DECL (c);
13000 if (ort != C_ORT_OMP_DECLARE_SIMD
13001 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13002 {
13003 error_at (OMP_CLAUSE_LOCATION (c),
13004 "modifier should not be specified in %<linear%> "
13005 "clause on %<simd%> or %<for%> constructs");
13006 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13007 }
13008 if (ort & C_ORT_CILK)
13009 {
13010 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13011 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13012 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13013 {
13014 error_at (OMP_CLAUSE_LOCATION (c),
13015 "linear clause applied to non-integral, "
13016 "non-floating, non-pointer variable with type %qT",
13017 TREE_TYPE (t));
13018 remove = true;
13019 break;
13020 }
13021 }
13022 else
13023 {
13024 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13025 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13026 {
13027 error_at (OMP_CLAUSE_LOCATION (c),
13028 "linear clause applied to non-integral non-pointer "
13029 "variable with type %qT", TREE_TYPE (t));
13030 remove = true;
13031 break;
13032 }
13033 if (TYPE_ATOMIC (TREE_TYPE (t)))
13034 {
13035 error_at (OMP_CLAUSE_LOCATION (c),
13036 "%<_Atomic%> %qD in %<linear%> clause", t);
13037 remove = true;
13038 break;
13039 }
13040 }
13041 if (ort == C_ORT_OMP_DECLARE_SIMD)
13042 {
13043 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13044 if (TREE_CODE (s) == PARM_DECL)
13045 {
13046 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13047 /* map_head bitmap is used as uniform_head if
13048 declare_simd. */
13049 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13050 linear_variable_step_check = true;
13051 goto check_dup_generic;
13052 }
13053 if (TREE_CODE (s) != INTEGER_CST)
13054 {
13055 error_at (OMP_CLAUSE_LOCATION (c),
13056 "%<linear%> clause step %qE is neither constant "
13057 "nor a parameter", s);
13058 remove = true;
13059 break;
13060 }
13061 }
13062 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13063 {
13064 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13065 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13066 OMP_CLAUSE_DECL (c), s);
13067 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13068 sizetype, fold_convert (sizetype, s),
13069 fold_convert
13070 (sizetype, OMP_CLAUSE_DECL (c)));
13071 if (s == error_mark_node)
13072 s = size_one_node;
13073 OMP_CLAUSE_LINEAR_STEP (c) = s;
13074 }
13075 else
13076 OMP_CLAUSE_LINEAR_STEP (c)
13077 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
13078 goto check_dup_generic;
13079
13080 check_dup_generic:
13081 t = OMP_CLAUSE_DECL (c);
13082 check_dup_generic_t:
13083 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13084 {
13085 error_at (OMP_CLAUSE_LOCATION (c),
13086 "%qE is not a variable in clause %qs", t,
13087 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13088 remove = true;
13089 }
13090 else if (ort == C_ORT_ACC
13091 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13092 {
13093 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13094 {
13095 error ("%qD appears more than once in reduction clauses", t);
13096 remove = true;
13097 }
13098 else
13099 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13100 }
13101 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13102 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13103 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13104 {
13105 error_at (OMP_CLAUSE_LOCATION (c),
13106 "%qE appears more than once in data clauses", t);
13107 remove = true;
13108 }
13109 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13110 && bitmap_bit_p (&map_head, DECL_UID (t)))
13111 {
13112 if (ort == C_ORT_ACC)
13113 error ("%qD appears more than once in data clauses", t);
13114 else
13115 error ("%qD appears both in data and map clauses", t);
13116 remove = true;
13117 }
13118 else
13119 bitmap_set_bit (&generic_head, DECL_UID (t));
13120 break;
13121
13122 case OMP_CLAUSE_FIRSTPRIVATE:
13123 t = OMP_CLAUSE_DECL (c);
13124 need_complete = true;
13125 need_implicitly_determined = true;
13126 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13127 {
13128 error_at (OMP_CLAUSE_LOCATION (c),
13129 "%qE is not a variable in clause %<firstprivate%>", t);
13130 remove = true;
13131 }
13132 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13133 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13134 {
13135 error_at (OMP_CLAUSE_LOCATION (c),
13136 "%qE appears more than once in data clauses", t);
13137 remove = true;
13138 }
13139 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13140 {
13141 if (ort == C_ORT_ACC)
13142 error ("%qD appears more than once in data clauses", t);
13143 else
13144 error ("%qD appears both in data and map clauses", t);
13145 remove = true;
13146 }
13147 else
13148 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13149 break;
13150
13151 case OMP_CLAUSE_LASTPRIVATE:
13152 t = OMP_CLAUSE_DECL (c);
13153 need_complete = true;
13154 need_implicitly_determined = true;
13155 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13156 {
13157 error_at (OMP_CLAUSE_LOCATION (c),
13158 "%qE is not a variable in clause %<lastprivate%>", t);
13159 remove = true;
13160 }
13161 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13162 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13163 {
13164 error_at (OMP_CLAUSE_LOCATION (c),
13165 "%qE appears more than once in data clauses", t);
13166 remove = true;
13167 }
13168 else
13169 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13170 break;
13171
13172 case OMP_CLAUSE_ALIGNED:
13173 t = OMP_CLAUSE_DECL (c);
13174 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13175 {
13176 error_at (OMP_CLAUSE_LOCATION (c),
13177 "%qE is not a variable in %<aligned%> clause", t);
13178 remove = true;
13179 }
13180 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13181 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13182 {
13183 error_at (OMP_CLAUSE_LOCATION (c),
13184 "%qE in %<aligned%> clause is neither a pointer nor "
13185 "an array", t);
13186 remove = true;
13187 }
13188 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13189 {
13190 error_at (OMP_CLAUSE_LOCATION (c),
13191 "%<_Atomic%> %qD in %<aligned%> clause", t);
13192 remove = true;
13193 break;
13194 }
13195 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13196 {
13197 error_at (OMP_CLAUSE_LOCATION (c),
13198 "%qE appears more than once in %<aligned%> clauses",
13199 t);
13200 remove = true;
13201 }
13202 else
13203 bitmap_set_bit (&aligned_head, DECL_UID (t));
13204 break;
13205
13206 case OMP_CLAUSE_DEPEND:
13207 t = OMP_CLAUSE_DECL (c);
13208 if (t == NULL_TREE)
13209 {
13210 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13211 == OMP_CLAUSE_DEPEND_SOURCE);
13212 break;
13213 }
13214 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13215 {
13216 gcc_assert (TREE_CODE (t) == TREE_LIST);
13217 for (; t; t = TREE_CHAIN (t))
13218 {
13219 tree decl = TREE_VALUE (t);
13220 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13221 {
13222 tree offset = TREE_PURPOSE (t);
13223 bool neg = wi::neg_p ((wide_int) offset);
13224 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13225 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13226 neg ? MINUS_EXPR : PLUS_EXPR,
13227 decl, offset);
13228 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13229 sizetype,
13230 fold_convert (sizetype, t2),
13231 fold_convert (sizetype, decl));
13232 if (t2 == error_mark_node)
13233 {
13234 remove = true;
13235 break;
13236 }
13237 TREE_PURPOSE (t) = t2;
13238 }
13239 }
13240 break;
13241 }
13242 if (TREE_CODE (t) == TREE_LIST)
13243 {
13244 if (handle_omp_array_sections (c, ort))
13245 remove = true;
13246 break;
13247 }
13248 if (t == error_mark_node)
13249 remove = true;
13250 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13251 {
13252 error_at (OMP_CLAUSE_LOCATION (c),
13253 "%qE is not a variable in %<depend%> clause", t);
13254 remove = true;
13255 }
13256 else if (!c_mark_addressable (t))
13257 remove = true;
13258 break;
13259
13260 case OMP_CLAUSE_MAP:
13261 case OMP_CLAUSE_TO:
13262 case OMP_CLAUSE_FROM:
13263 case OMP_CLAUSE__CACHE_:
13264 t = OMP_CLAUSE_DECL (c);
13265 if (TREE_CODE (t) == TREE_LIST)
13266 {
13267 if (handle_omp_array_sections (c, ort))
13268 remove = true;
13269 else
13270 {
13271 t = OMP_CLAUSE_DECL (c);
13272 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13273 {
13274 error_at (OMP_CLAUSE_LOCATION (c),
13275 "array section does not have mappable type "
13276 "in %qs clause",
13277 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13278 remove = true;
13279 }
13280 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13281 {
13282 error_at (OMP_CLAUSE_LOCATION (c),
13283 "%<_Atomic%> %qE in %qs clause", t,
13284 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13285 remove = true;
13286 }
13287 while (TREE_CODE (t) == ARRAY_REF)
13288 t = TREE_OPERAND (t, 0);
13289 if (TREE_CODE (t) == COMPONENT_REF
13290 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13291 {
13292 while (TREE_CODE (t) == COMPONENT_REF)
13293 t = TREE_OPERAND (t, 0);
13294 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13295 break;
13296 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13297 {
13298 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13299 error ("%qD appears more than once in motion"
13300 " clauses", t);
13301 else if (ort == C_ORT_ACC)
13302 error ("%qD appears more than once in data"
13303 " clauses", t);
13304 else
13305 error ("%qD appears more than once in map"
13306 " clauses", t);
13307 remove = true;
13308 }
13309 else
13310 {
13311 bitmap_set_bit (&map_head, DECL_UID (t));
13312 bitmap_set_bit (&map_field_head, DECL_UID (t));
13313 }
13314 }
13315 }
13316 break;
13317 }
13318 if (t == error_mark_node)
13319 {
13320 remove = true;
13321 break;
13322 }
13323 if (TREE_CODE (t) == COMPONENT_REF
13324 && (ort & C_ORT_OMP)
13325 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13326 {
13327 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13328 {
13329 error_at (OMP_CLAUSE_LOCATION (c),
13330 "bit-field %qE in %qs clause",
13331 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13332 remove = true;
13333 }
13334 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13335 {
13336 error_at (OMP_CLAUSE_LOCATION (c),
13337 "%qE does not have a mappable type in %qs clause",
13338 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13339 remove = true;
13340 }
13341 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13342 {
13343 error_at (OMP_CLAUSE_LOCATION (c),
13344 "%<_Atomic%> %qE in %qs clause", t,
13345 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13346 remove = true;
13347 }
13348 while (TREE_CODE (t) == COMPONENT_REF)
13349 {
13350 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13351 == UNION_TYPE)
13352 {
13353 error_at (OMP_CLAUSE_LOCATION (c),
13354 "%qE is a member of a union", t);
13355 remove = true;
13356 break;
13357 }
13358 t = TREE_OPERAND (t, 0);
13359 }
13360 if (remove)
13361 break;
13362 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13363 {
13364 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13365 break;
13366 }
13367 }
13368 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13369 {
13370 error_at (OMP_CLAUSE_LOCATION (c),
13371 "%qE is not a variable in %qs clause", t,
13372 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13373 remove = true;
13374 }
13375 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13376 {
13377 error_at (OMP_CLAUSE_LOCATION (c),
13378 "%qD is threadprivate variable in %qs clause", t,
13379 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13380 remove = true;
13381 }
13382 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13383 || (OMP_CLAUSE_MAP_KIND (c)
13384 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13385 && !c_mark_addressable (t))
13386 remove = true;
13387 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13388 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13389 || (OMP_CLAUSE_MAP_KIND (c)
13390 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13391 || (OMP_CLAUSE_MAP_KIND (c)
13392 == GOMP_MAP_FORCE_DEVICEPTR)))
13393 && t == OMP_CLAUSE_DECL (c)
13394 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13395 {
13396 error_at (OMP_CLAUSE_LOCATION (c),
13397 "%qD does not have a mappable type in %qs clause", t,
13398 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13399 remove = true;
13400 }
13401 else if (TREE_TYPE (t) == error_mark_node)
13402 remove = true;
13403 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13404 {
13405 error_at (OMP_CLAUSE_LOCATION (c),
13406 "%<_Atomic%> %qE in %qs clause", t,
13407 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13408 remove = true;
13409 }
13410 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13411 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13412 {
13413 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13414 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13415 {
13416 error ("%qD appears more than once in data clauses", t);
13417 remove = true;
13418 }
13419 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13420 {
13421 if (ort == C_ORT_ACC)
13422 error ("%qD appears more than once in data clauses", t);
13423 else
13424 error ("%qD appears both in data and map clauses", t);
13425 remove = true;
13426 }
13427 else
13428 bitmap_set_bit (&generic_head, DECL_UID (t));
13429 }
13430 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13431 {
13432 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13433 error ("%qD appears more than once in motion clauses", t);
13434 else if (ort == C_ORT_ACC)
13435 error ("%qD appears more than once in data clauses", t);
13436 else
13437 error ("%qD appears more than once in map clauses", t);
13438 remove = true;
13439 }
13440 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13441 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13442 {
13443 if (ort == C_ORT_ACC)
13444 error ("%qD appears more than once in data clauses", t);
13445 else
13446 error ("%qD appears both in data and map clauses", t);
13447 remove = true;
13448 }
13449 else
13450 {
13451 bitmap_set_bit (&map_head, DECL_UID (t));
13452 if (t != OMP_CLAUSE_DECL (c)
13453 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13454 bitmap_set_bit (&map_field_head, DECL_UID (t));
13455 }
13456 break;
13457
13458 case OMP_CLAUSE_TO_DECLARE:
13459 case OMP_CLAUSE_LINK:
13460 t = OMP_CLAUSE_DECL (c);
13461 if (TREE_CODE (t) == FUNCTION_DECL
13462 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13463 ;
13464 else if (!VAR_P (t))
13465 {
13466 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13467 error_at (OMP_CLAUSE_LOCATION (c),
13468 "%qE is neither a variable nor a function name in "
13469 "clause %qs", t,
13470 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13471 else
13472 error_at (OMP_CLAUSE_LOCATION (c),
13473 "%qE is not a variable in clause %qs", t,
13474 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13475 remove = true;
13476 }
13477 else if (DECL_THREAD_LOCAL_P (t))
13478 {
13479 error_at (OMP_CLAUSE_LOCATION (c),
13480 "%qD is threadprivate variable in %qs clause", t,
13481 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13482 remove = true;
13483 }
13484 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13485 {
13486 error_at (OMP_CLAUSE_LOCATION (c),
13487 "%qD does not have a mappable type in %qs clause", t,
13488 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13489 remove = true;
13490 }
13491 if (remove)
13492 break;
13493 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13494 {
13495 error_at (OMP_CLAUSE_LOCATION (c),
13496 "%qE appears more than once on the same "
13497 "%<declare target%> directive", t);
13498 remove = true;
13499 }
13500 else
13501 bitmap_set_bit (&generic_head, DECL_UID (t));
13502 break;
13503
13504 case OMP_CLAUSE_UNIFORM:
13505 t = OMP_CLAUSE_DECL (c);
13506 if (TREE_CODE (t) != PARM_DECL)
13507 {
13508 if (DECL_P (t))
13509 error_at (OMP_CLAUSE_LOCATION (c),
13510 "%qD is not an argument in %<uniform%> clause", t);
13511 else
13512 error_at (OMP_CLAUSE_LOCATION (c),
13513 "%qE is not an argument in %<uniform%> clause", t);
13514 remove = true;
13515 break;
13516 }
13517 /* map_head bitmap is used as uniform_head if declare_simd. */
13518 bitmap_set_bit (&map_head, DECL_UID (t));
13519 goto check_dup_generic;
13520
13521 case OMP_CLAUSE_IS_DEVICE_PTR:
13522 case OMP_CLAUSE_USE_DEVICE_PTR:
13523 t = OMP_CLAUSE_DECL (c);
13524 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13525 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13526 {
13527 error_at (OMP_CLAUSE_LOCATION (c),
13528 "%qs variable is neither a pointer nor an array",
13529 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13530 remove = true;
13531 }
13532 goto check_dup_generic;
13533
13534 case OMP_CLAUSE_NOWAIT:
13535 if (copyprivate_seen)
13536 {
13537 error_at (OMP_CLAUSE_LOCATION (c),
13538 "%<nowait%> clause must not be used together "
13539 "with %<copyprivate%>");
13540 remove = true;
13541 break;
13542 }
13543 nowait_clause = pc;
13544 pc = &OMP_CLAUSE_CHAIN (c);
13545 continue;
13546
13547 case OMP_CLAUSE_IF:
13548 case OMP_CLAUSE_NUM_THREADS:
13549 case OMP_CLAUSE_NUM_TEAMS:
13550 case OMP_CLAUSE_THREAD_LIMIT:
13551 case OMP_CLAUSE_DEFAULT:
13552 case OMP_CLAUSE_UNTIED:
13553 case OMP_CLAUSE_COLLAPSE:
13554 case OMP_CLAUSE_FINAL:
13555 case OMP_CLAUSE_MERGEABLE:
13556 case OMP_CLAUSE_DEVICE:
13557 case OMP_CLAUSE_DIST_SCHEDULE:
13558 case OMP_CLAUSE_PARALLEL:
13559 case OMP_CLAUSE_FOR:
13560 case OMP_CLAUSE_SECTIONS:
13561 case OMP_CLAUSE_TASKGROUP:
13562 case OMP_CLAUSE_PROC_BIND:
13563 case OMP_CLAUSE_PRIORITY:
13564 case OMP_CLAUSE_GRAINSIZE:
13565 case OMP_CLAUSE_NUM_TASKS:
13566 case OMP_CLAUSE_NOGROUP:
13567 case OMP_CLAUSE_THREADS:
13568 case OMP_CLAUSE_SIMD:
13569 case OMP_CLAUSE_HINT:
13570 case OMP_CLAUSE_DEFAULTMAP:
13571 case OMP_CLAUSE__CILK_FOR_COUNT_:
13572 case OMP_CLAUSE_NUM_GANGS:
13573 case OMP_CLAUSE_NUM_WORKERS:
13574 case OMP_CLAUSE_VECTOR_LENGTH:
13575 case OMP_CLAUSE_ASYNC:
13576 case OMP_CLAUSE_WAIT:
13577 case OMP_CLAUSE_AUTO:
13578 case OMP_CLAUSE_INDEPENDENT:
13579 case OMP_CLAUSE_SEQ:
13580 case OMP_CLAUSE_GANG:
13581 case OMP_CLAUSE_WORKER:
13582 case OMP_CLAUSE_VECTOR:
13583 case OMP_CLAUSE_TILE:
13584 pc = &OMP_CLAUSE_CHAIN (c);
13585 continue;
13586
13587 case OMP_CLAUSE_SCHEDULE:
13588 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13589 {
13590 const char *p = NULL;
13591 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13592 {
13593 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13594 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13595 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13596 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13597 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13598 default: gcc_unreachable ();
13599 }
13600 if (p)
13601 {
13602 error_at (OMP_CLAUSE_LOCATION (c),
13603 "%<nonmonotonic%> modifier specified for %qs "
13604 "schedule kind", p);
13605 OMP_CLAUSE_SCHEDULE_KIND (c)
13606 = (enum omp_clause_schedule_kind)
13607 (OMP_CLAUSE_SCHEDULE_KIND (c)
13608 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13609 }
13610 }
13611 schedule_clause = c;
13612 pc = &OMP_CLAUSE_CHAIN (c);
13613 continue;
13614
13615 case OMP_CLAUSE_ORDERED:
13616 ordered_seen = true;
13617 pc = &OMP_CLAUSE_CHAIN (c);
13618 continue;
13619
13620 case OMP_CLAUSE_SAFELEN:
13621 safelen = c;
13622 pc = &OMP_CLAUSE_CHAIN (c);
13623 continue;
13624 case OMP_CLAUSE_SIMDLEN:
13625 simdlen = c;
13626 pc = &OMP_CLAUSE_CHAIN (c);
13627 continue;
13628
13629 case OMP_CLAUSE_INBRANCH:
13630 case OMP_CLAUSE_NOTINBRANCH:
13631 if (branch_seen)
13632 {
13633 error_at (OMP_CLAUSE_LOCATION (c),
13634 "%<inbranch%> clause is incompatible with "
13635 "%<notinbranch%>");
13636 remove = true;
13637 break;
13638 }
13639 branch_seen = true;
13640 pc = &OMP_CLAUSE_CHAIN (c);
13641 continue;
13642
13643 default:
13644 gcc_unreachable ();
13645 }
13646
13647 if (!remove)
13648 {
13649 t = OMP_CLAUSE_DECL (c);
13650
13651 if (need_complete)
13652 {
13653 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13654 if (t == error_mark_node)
13655 remove = true;
13656 }
13657
13658 if (need_implicitly_determined)
13659 {
13660 const char *share_name = NULL;
13661
13662 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13663 share_name = "threadprivate";
13664 else switch (c_omp_predetermined_sharing (t))
13665 {
13666 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13667 break;
13668 case OMP_CLAUSE_DEFAULT_SHARED:
13669 /* const vars may be specified in firstprivate clause. */
13670 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13671 && TREE_READONLY (t))
13672 break;
13673 share_name = "shared";
13674 break;
13675 case OMP_CLAUSE_DEFAULT_PRIVATE:
13676 share_name = "private";
13677 break;
13678 default:
13679 gcc_unreachable ();
13680 }
13681 if (share_name)
13682 {
13683 error_at (OMP_CLAUSE_LOCATION (c),
13684 "%qE is predetermined %qs for %qs",
13685 t, share_name,
13686 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13687 remove = true;
13688 }
13689 }
13690 }
13691
13692 if (remove)
13693 *pc = OMP_CLAUSE_CHAIN (c);
13694 else
13695 pc = &OMP_CLAUSE_CHAIN (c);
13696 }
13697
13698 if (simdlen
13699 && safelen
13700 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13701 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13702 {
13703 error_at (OMP_CLAUSE_LOCATION (simdlen),
13704 "%<simdlen%> clause value is bigger than "
13705 "%<safelen%> clause value");
13706 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13707 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13708 }
13709
13710 if (ordered_seen
13711 && schedule_clause
13712 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13713 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13714 {
13715 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13716 "%<nonmonotonic%> schedule modifier specified together "
13717 "with %<ordered%> clause");
13718 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13719 = (enum omp_clause_schedule_kind)
13720 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13721 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13722 }
13723
13724 if (linear_variable_step_check)
13725 for (pc = &clauses, c = clauses; c ; c = *pc)
13726 {
13727 bool remove = false;
13728 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13729 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13730 && !bitmap_bit_p (&map_head,
13731 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13732 {
13733 error_at (OMP_CLAUSE_LOCATION (c),
13734 "%<linear%> clause step is a parameter %qD not "
13735 "specified in %<uniform%> clause",
13736 OMP_CLAUSE_LINEAR_STEP (c));
13737 remove = true;
13738 }
13739
13740 if (remove)
13741 *pc = OMP_CLAUSE_CHAIN (c);
13742 else
13743 pc = &OMP_CLAUSE_CHAIN (c);
13744 }
13745
13746 bitmap_obstack_release (NULL);
13747 return clauses;
13748 }
13749
13750 /* Return code to initialize DST with a copy constructor from SRC.
13751 C doesn't have copy constructors nor assignment operators, only for
13752 _Atomic vars we need to perform __atomic_load from src into a temporary
13753 followed by __atomic_store of the temporary to dst. */
13754
13755 tree
13756 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
13757 {
13758 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
13759 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13760
13761 location_t loc = OMP_CLAUSE_LOCATION (clause);
13762 tree type = TREE_TYPE (dst);
13763 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
13764 tree tmp = create_tmp_var (nonatomic_type);
13765 tree tmp_addr = build_fold_addr_expr (tmp);
13766 TREE_ADDRESSABLE (tmp) = 1;
13767 TREE_NO_WARNING (tmp) = 1;
13768 tree src_addr = build_fold_addr_expr (src);
13769 tree dst_addr = build_fold_addr_expr (dst);
13770 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
13771 vec<tree, va_gc> *params;
13772 /* Expansion of a generic atomic load may require an addition
13773 element, so allocate enough to prevent a resize. */
13774 vec_alloc (params, 4);
13775
13776 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
13777 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
13778 params->quick_push (src_addr);
13779 params->quick_push (tmp_addr);
13780 params->quick_push (seq_cst);
13781 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13782
13783 vec_alloc (params, 4);
13784
13785 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
13786 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
13787 params->quick_push (dst_addr);
13788 params->quick_push (tmp_addr);
13789 params->quick_push (seq_cst);
13790 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13791 return build2 (COMPOUND_EXPR, void_type_node, load, store);
13792 }
13793
13794 /* Create a transaction node. */
13795
13796 tree
13797 c_finish_transaction (location_t loc, tree block, int flags)
13798 {
13799 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13800 if (flags & TM_STMT_ATTR_OUTER)
13801 TRANSACTION_EXPR_OUTER (stmt) = 1;
13802 if (flags & TM_STMT_ATTR_RELAXED)
13803 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13804 return add_stmt (stmt);
13805 }
13806
13807 /* Make a variant type in the proper way for C/C++, propagating qualifiers
13808 down to the element type of an array. If ORIG_QUAL_TYPE is not
13809 NULL, then it should be used as the qualified type
13810 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13811 preserve information about the typedef name from which an array
13812 type was derived). */
13813
13814 tree
13815 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
13816 size_t orig_qual_indirect)
13817 {
13818 if (type == error_mark_node)
13819 return type;
13820
13821 if (TREE_CODE (type) == ARRAY_TYPE)
13822 {
13823 tree t;
13824 tree element_type = c_build_qualified_type (TREE_TYPE (type),
13825 type_quals, orig_qual_type,
13826 orig_qual_indirect - 1);
13827
13828 /* See if we already have an identically qualified type. */
13829 if (orig_qual_type && orig_qual_indirect == 0)
13830 t = orig_qual_type;
13831 else
13832 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13833 {
13834 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13835 && TYPE_NAME (t) == TYPE_NAME (type)
13836 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13837 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13838 TYPE_ATTRIBUTES (type)))
13839 break;
13840 }
13841 if (!t)
13842 {
13843 tree domain = TYPE_DOMAIN (type);
13844
13845 t = build_variant_type_copy (type);
13846 TREE_TYPE (t) = element_type;
13847
13848 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13849 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13850 SET_TYPE_STRUCTURAL_EQUALITY (t);
13851 else if (TYPE_CANONICAL (element_type) != element_type
13852 || (domain && TYPE_CANONICAL (domain) != domain))
13853 {
13854 tree unqualified_canon
13855 = build_array_type (TYPE_CANONICAL (element_type),
13856 domain? TYPE_CANONICAL (domain)
13857 : NULL_TREE);
13858 if (TYPE_REVERSE_STORAGE_ORDER (type))
13859 {
13860 unqualified_canon
13861 = build_distinct_type_copy (unqualified_canon);
13862 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13863 }
13864 TYPE_CANONICAL (t)
13865 = c_build_qualified_type (unqualified_canon, type_quals);
13866 }
13867 else
13868 TYPE_CANONICAL (t) = t;
13869 }
13870 return t;
13871 }
13872
13873 /* A restrict-qualified pointer type must be a pointer to object or
13874 incomplete type. Note that the use of POINTER_TYPE_P also allows
13875 REFERENCE_TYPEs, which is appropriate for C++. */
13876 if ((type_quals & TYPE_QUAL_RESTRICT)
13877 && (!POINTER_TYPE_P (type)
13878 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13879 {
13880 error ("invalid use of %<restrict%>");
13881 type_quals &= ~TYPE_QUAL_RESTRICT;
13882 }
13883
13884 tree var_type = (orig_qual_type && orig_qual_indirect == 0
13885 ? orig_qual_type
13886 : build_qualified_type (type, type_quals));
13887 /* A variant type does not inherit the list of incomplete vars from the
13888 type main variant. */
13889 if (RECORD_OR_UNION_TYPE_P (var_type)
13890 && TYPE_MAIN_VARIANT (var_type) != var_type)
13891 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13892 return var_type;
13893 }
13894
13895 /* Build a VA_ARG_EXPR for the C parser. */
13896
13897 tree
13898 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
13899 {
13900 if (error_operand_p (type))
13901 return error_mark_node;
13902 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13903 order because it takes the address of the expression. */
13904 else if (handled_component_p (expr)
13905 && reverse_storage_order_for_component_p (expr))
13906 {
13907 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
13908 return error_mark_node;
13909 }
13910 else if (!COMPLETE_TYPE_P (type))
13911 {
13912 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
13913 "type %qT", type);
13914 return error_mark_node;
13915 }
13916 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
13917 warning_at (loc2, OPT_Wc___compat,
13918 "C++ requires promoted type, not enum type, in %<va_arg%>");
13919 return build_va_arg (loc2, expr, type);
13920 }
13921
13922 /* Return truthvalue of whether T1 is the same tree structure as T2.
13923 Return 1 if they are the same. Return 0 if they are different. */
13924
13925 bool
13926 c_tree_equal (tree t1, tree t2)
13927 {
13928 enum tree_code code1, code2;
13929
13930 if (t1 == t2)
13931 return true;
13932 if (!t1 || !t2)
13933 return false;
13934
13935 for (code1 = TREE_CODE (t1);
13936 CONVERT_EXPR_CODE_P (code1)
13937 || code1 == NON_LVALUE_EXPR;
13938 code1 = TREE_CODE (t1))
13939 t1 = TREE_OPERAND (t1, 0);
13940 for (code2 = TREE_CODE (t2);
13941 CONVERT_EXPR_CODE_P (code2)
13942 || code2 == NON_LVALUE_EXPR;
13943 code2 = TREE_CODE (t2))
13944 t2 = TREE_OPERAND (t2, 0);
13945
13946 /* They might have become equal now. */
13947 if (t1 == t2)
13948 return true;
13949
13950 if (code1 != code2)
13951 return false;
13952
13953 switch (code1)
13954 {
13955 case INTEGER_CST:
13956 return wi::eq_p (t1, t2);
13957
13958 case REAL_CST:
13959 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
13960
13961 case STRING_CST:
13962 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
13963 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
13964 TREE_STRING_LENGTH (t1));
13965
13966 case FIXED_CST:
13967 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
13968 TREE_FIXED_CST (t2));
13969
13970 case COMPLEX_CST:
13971 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
13972 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
13973
13974 case VECTOR_CST:
13975 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
13976
13977 case CONSTRUCTOR:
13978 /* We need to do this when determining whether or not two
13979 non-type pointer to member function template arguments
13980 are the same. */
13981 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
13982 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
13983 return false;
13984 {
13985 tree field, value;
13986 unsigned int i;
13987 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
13988 {
13989 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
13990 if (!c_tree_equal (field, elt2->index)
13991 || !c_tree_equal (value, elt2->value))
13992 return false;
13993 }
13994 }
13995 return true;
13996
13997 case TREE_LIST:
13998 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
13999 return false;
14000 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14001 return false;
14002 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14003
14004 case SAVE_EXPR:
14005 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14006
14007 case CALL_EXPR:
14008 {
14009 tree arg1, arg2;
14010 call_expr_arg_iterator iter1, iter2;
14011 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14012 return false;
14013 for (arg1 = first_call_expr_arg (t1, &iter1),
14014 arg2 = first_call_expr_arg (t2, &iter2);
14015 arg1 && arg2;
14016 arg1 = next_call_expr_arg (&iter1),
14017 arg2 = next_call_expr_arg (&iter2))
14018 if (!c_tree_equal (arg1, arg2))
14019 return false;
14020 if (arg1 || arg2)
14021 return false;
14022 return true;
14023 }
14024
14025 case TARGET_EXPR:
14026 {
14027 tree o1 = TREE_OPERAND (t1, 0);
14028 tree o2 = TREE_OPERAND (t2, 0);
14029
14030 /* Special case: if either target is an unallocated VAR_DECL,
14031 it means that it's going to be unified with whatever the
14032 TARGET_EXPR is really supposed to initialize, so treat it
14033 as being equivalent to anything. */
14034 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
14035 && !DECL_RTL_SET_P (o1))
14036 /*Nop*/;
14037 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
14038 && !DECL_RTL_SET_P (o2))
14039 /*Nop*/;
14040 else if (!c_tree_equal (o1, o2))
14041 return false;
14042
14043 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14044 }
14045
14046 case COMPONENT_REF:
14047 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14048 return false;
14049 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14050
14051 case PARM_DECL:
14052 case VAR_DECL:
14053 case CONST_DECL:
14054 case FIELD_DECL:
14055 case FUNCTION_DECL:
14056 case IDENTIFIER_NODE:
14057 case SSA_NAME:
14058 return false;
14059
14060 case TREE_VEC:
14061 {
14062 unsigned ix;
14063 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14064 return false;
14065 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14066 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14067 TREE_VEC_ELT (t2, ix)))
14068 return false;
14069 return true;
14070 }
14071
14072 default:
14073 break;
14074 }
14075
14076 switch (TREE_CODE_CLASS (code1))
14077 {
14078 case tcc_unary:
14079 case tcc_binary:
14080 case tcc_comparison:
14081 case tcc_expression:
14082 case tcc_vl_exp:
14083 case tcc_reference:
14084 case tcc_statement:
14085 {
14086 int i, n = TREE_OPERAND_LENGTH (t1);
14087
14088 switch (code1)
14089 {
14090 case PREINCREMENT_EXPR:
14091 case PREDECREMENT_EXPR:
14092 case POSTINCREMENT_EXPR:
14093 case POSTDECREMENT_EXPR:
14094 n = 1;
14095 break;
14096 case ARRAY_REF:
14097 n = 2;
14098 break;
14099 default:
14100 break;
14101 }
14102
14103 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14104 && n != TREE_OPERAND_LENGTH (t2))
14105 return false;
14106
14107 for (i = 0; i < n; ++i)
14108 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14109 return false;
14110
14111 return true;
14112 }
14113
14114 case tcc_type:
14115 return comptypes (t1, t2);
14116 default:
14117 gcc_unreachable ();
14118 }
14119 /* We can get here with --disable-checking. */
14120 return false;
14121 }
14122
14123 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
14124 spawn-helper and BODY is the newly created body for FNDECL. */
14125
14126 void
14127 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
14128 {
14129 tree list = alloc_stmt_list ();
14130 tree frame = make_cilk_frame (fndecl);
14131 tree dtor = create_cilk_function_exit (frame, false, true);
14132 add_local_decl (cfun, frame);
14133
14134 DECL_SAVED_TREE (fndecl) = list;
14135 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
14136 frame);
14137 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
14138 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
14139
14140 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
14141 append_to_statement_list (detach_expr, &body_list);
14142
14143 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
14144 body = fold_build_cleanup_point_expr (void_type_node, body);
14145
14146 append_to_statement_list (body, &body_list);
14147 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
14148 body_list, dtor), &list);
14149 }
14150
14151 /* Returns true when the function declaration FNDECL is implicit,
14152 introduced as a result of a call to an otherwise undeclared
14153 function, and false otherwise. */
14154
14155 bool
14156 c_decl_implicit (const_tree fndecl)
14157 {
14158 return C_DECL_IMPLICIT (fndecl);
14159 }