]> gcc.gnu.org Git - gcc.git/blob - gcc/c/c-typeck.c
re PR c/64709 (Bogus -Wmissing-field-initializers warning)
[gcc.git] / gcc / c / c-typeck.c
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2015 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 "tm.h"
30 #include "hash-set.h"
31 #include "vec.h"
32 #include "symtab.h"
33 #include "input.h"
34 #include "alias.h"
35 #include "double-int.h"
36 #include "machmode.h"
37 #include "inchash.h"
38 #include "real.h"
39 #include "fixed-value.h"
40 #include "tree.h"
41 #include "fold-const.h"
42 #include "stor-layout.h"
43 #include "trans-mem.h"
44 #include "varasm.h"
45 #include "stmt.h"
46 #include "langhooks.h"
47 #include "c-tree.h"
48 #include "c-lang.h"
49 #include "flags.h"
50 #include "intl.h"
51 #include "target.h"
52 #include "tree-iterator.h"
53 #include "bitmap.h"
54 #include "predict.h"
55 #include "vec.h"
56 #include "hashtab.h"
57 #include "hash-set.h"
58 #include "machmode.h"
59 #include "hard-reg-set.h"
60 #include "input.h"
61 #include "function.h"
62 #include "gimple-expr.h"
63 #include "gimplify.h"
64 #include "tree-inline.h"
65 #include "omp-low.h"
66 #include "c-family/c-objc.h"
67 #include "c-family/c-common.h"
68 #include "c-family/c-ubsan.h"
69 #include "cilk.h"
70 #include "wide-int.h"
71 #include "gomp-constants.h"
72
73 /* Possible cases of implicit bad conversions. Used to select
74 diagnostic messages in convert_for_assignment. */
75 enum impl_conv {
76 ic_argpass,
77 ic_assign,
78 ic_init,
79 ic_return
80 };
81
82 /* The level of nesting inside "__alignof__". */
83 int in_alignof;
84
85 /* The level of nesting inside "sizeof". */
86 int in_sizeof;
87
88 /* The level of nesting inside "typeof". */
89 int in_typeof;
90
91 /* The argument of last parsed sizeof expression, only to be tested
92 if expr.original_code == SIZEOF_EXPR. */
93 tree c_last_sizeof_arg;
94
95 /* Nonzero if we might need to print a "missing braces around
96 initializer" message within this initializer. */
97 static int found_missing_braces;
98
99 static int require_constant_value;
100 static int require_constant_elements;
101
102 static bool null_pointer_constant_p (const_tree);
103 static tree qualify_type (tree, tree);
104 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
105 bool *);
106 static int comp_target_types (location_t, tree, tree);
107 static int function_types_compatible_p (const_tree, const_tree, bool *,
108 bool *);
109 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
110 static tree lookup_field (tree, tree);
111 static int convert_arguments (location_t, vec<location_t>, tree,
112 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
113 tree);
114 static tree pointer_diff (location_t, tree, tree);
115 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
116 enum impl_conv, bool, tree, tree, int);
117 static tree valid_compound_expr_initializer (tree, tree);
118 static void push_string (const char *);
119 static void push_member_name (tree);
120 static int spelling_length (void);
121 static char *print_spelling (char *);
122 static void warning_init (location_t, int, const char *);
123 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
124 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
125 bool, struct obstack *);
126 static void output_pending_init_elements (int, struct obstack *);
127 static int set_designator (location_t, int, struct obstack *);
128 static void push_range_stack (tree, struct obstack *);
129 static void add_pending_init (location_t, tree, tree, tree, bool,
130 struct obstack *);
131 static void set_nonincremental_init (struct obstack *);
132 static void set_nonincremental_init_from_string (tree, struct obstack *);
133 static tree find_init_member (tree, struct obstack *);
134 static void readonly_warning (tree, enum lvalue_use);
135 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
136 static void record_maybe_used_decl (tree);
137 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
138 \f
139 /* Return true if EXP is a null pointer constant, false otherwise. */
140
141 static bool
142 null_pointer_constant_p (const_tree expr)
143 {
144 /* This should really operate on c_expr structures, but they aren't
145 yet available everywhere required. */
146 tree type = TREE_TYPE (expr);
147 return (TREE_CODE (expr) == INTEGER_CST
148 && !TREE_OVERFLOW (expr)
149 && integer_zerop (expr)
150 && (INTEGRAL_TYPE_P (type)
151 || (TREE_CODE (type) == POINTER_TYPE
152 && VOID_TYPE_P (TREE_TYPE (type))
153 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
154 }
155
156 /* EXPR may appear in an unevaluated part of an integer constant
157 expression, but not in an evaluated part. Wrap it in a
158 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
159 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
160
161 static tree
162 note_integer_operands (tree expr)
163 {
164 tree ret;
165 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
166 {
167 ret = copy_node (expr);
168 TREE_OVERFLOW (ret) = 1;
169 }
170 else
171 {
172 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
173 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
174 }
175 return ret;
176 }
177
178 /* Having checked whether EXPR may appear in an unevaluated part of an
179 integer constant expression and found that it may, remove any
180 C_MAYBE_CONST_EXPR noting this fact and return the resulting
181 expression. */
182
183 static inline tree
184 remove_c_maybe_const_expr (tree expr)
185 {
186 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
187 return C_MAYBE_CONST_EXPR_EXPR (expr);
188 else
189 return expr;
190 }
191
192 \f/* This is a cache to hold if two types are compatible or not. */
193
194 struct tagged_tu_seen_cache {
195 const struct tagged_tu_seen_cache * next;
196 const_tree t1;
197 const_tree t2;
198 /* The return value of tagged_types_tu_compatible_p if we had seen
199 these two types already. */
200 int val;
201 };
202
203 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
204 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
205
206 /* Do `exp = require_complete_type (exp);' to make sure exp
207 does not have an incomplete type. (That includes void types.) */
208
209 tree
210 require_complete_type (tree value)
211 {
212 tree type = TREE_TYPE (value);
213
214 if (error_operand_p (value))
215 return error_mark_node;
216
217 /* First, detect a valid value with a complete type. */
218 if (COMPLETE_TYPE_P (type))
219 return value;
220
221 c_incomplete_type_error (value, type);
222 return error_mark_node;
223 }
224
225 /* Print an error message for invalid use of an incomplete type.
226 VALUE is the expression that was used (or 0 if that isn't known)
227 and TYPE is the type that was invalid. */
228
229 void
230 c_incomplete_type_error (const_tree value, const_tree type)
231 {
232 const char *type_code_string;
233
234 /* Avoid duplicate error message. */
235 if (TREE_CODE (type) == ERROR_MARK)
236 return;
237
238 if (value != 0 && (TREE_CODE (value) == VAR_DECL
239 || TREE_CODE (value) == PARM_DECL))
240 error ("%qD has an incomplete type", value);
241 else
242 {
243 retry:
244 /* We must print an error message. Be clever about what it says. */
245
246 switch (TREE_CODE (type))
247 {
248 case RECORD_TYPE:
249 type_code_string = "struct";
250 break;
251
252 case UNION_TYPE:
253 type_code_string = "union";
254 break;
255
256 case ENUMERAL_TYPE:
257 type_code_string = "enum";
258 break;
259
260 case VOID_TYPE:
261 error ("invalid use of void expression");
262 return;
263
264 case ARRAY_TYPE:
265 if (TYPE_DOMAIN (type))
266 {
267 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
268 {
269 error ("invalid use of flexible array member");
270 return;
271 }
272 type = TREE_TYPE (type);
273 goto retry;
274 }
275 error ("invalid use of array with unspecified bounds");
276 return;
277
278 default:
279 gcc_unreachable ();
280 }
281
282 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
283 error ("invalid use of undefined type %<%s %E%>",
284 type_code_string, TYPE_NAME (type));
285 else
286 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
287 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
288 }
289 }
290
291 /* Given a type, apply default promotions wrt unnamed function
292 arguments and return the new type. */
293
294 tree
295 c_type_promotes_to (tree type)
296 {
297 tree ret = NULL_TREE;
298
299 if (TYPE_MAIN_VARIANT (type) == float_type_node)
300 ret = double_type_node;
301 else if (c_promoting_integer_type_p (type))
302 {
303 /* Preserve unsignedness if not really getting any wider. */
304 if (TYPE_UNSIGNED (type)
305 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
306 ret = unsigned_type_node;
307 else
308 ret = integer_type_node;
309 }
310
311 if (ret != NULL_TREE)
312 return (TYPE_ATOMIC (type)
313 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
314 : ret);
315
316 return type;
317 }
318
319 /* Return true if between two named address spaces, whether there is a superset
320 named address space that encompasses both address spaces. If there is a
321 superset, return which address space is the superset. */
322
323 static bool
324 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
325 {
326 if (as1 == as2)
327 {
328 *common = as1;
329 return true;
330 }
331 else if (targetm.addr_space.subset_p (as1, as2))
332 {
333 *common = as2;
334 return true;
335 }
336 else if (targetm.addr_space.subset_p (as2, as1))
337 {
338 *common = as1;
339 return true;
340 }
341 else
342 return false;
343 }
344
345 /* Return a variant of TYPE which has all the type qualifiers of LIKE
346 as well as those of TYPE. */
347
348 static tree
349 qualify_type (tree type, tree like)
350 {
351 addr_space_t as_type = TYPE_ADDR_SPACE (type);
352 addr_space_t as_like = TYPE_ADDR_SPACE (like);
353 addr_space_t as_common;
354
355 /* If the two named address spaces are different, determine the common
356 superset address space. If there isn't one, raise an error. */
357 if (!addr_space_superset (as_type, as_like, &as_common))
358 {
359 as_common = as_type;
360 error ("%qT and %qT are in disjoint named address spaces",
361 type, like);
362 }
363
364 return c_build_qualified_type (type,
365 TYPE_QUALS_NO_ADDR_SPACE (type)
366 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
367 | ENCODE_QUAL_ADDR_SPACE (as_common));
368 }
369
370 /* Return true iff the given tree T is a variable length array. */
371
372 bool
373 c_vla_type_p (const_tree t)
374 {
375 if (TREE_CODE (t) == ARRAY_TYPE
376 && C_TYPE_VARIABLE_SIZE (t))
377 return true;
378 return false;
379 }
380 \f
381 /* Return the composite type of two compatible types.
382
383 We assume that comptypes has already been done and returned
384 nonzero; if that isn't so, this may crash. In particular, we
385 assume that qualifiers match. */
386
387 tree
388 composite_type (tree t1, tree t2)
389 {
390 enum tree_code code1;
391 enum tree_code code2;
392 tree attributes;
393
394 /* Save time if the two types are the same. */
395
396 if (t1 == t2) return t1;
397
398 /* If one type is nonsense, use the other. */
399 if (t1 == error_mark_node)
400 return t2;
401 if (t2 == error_mark_node)
402 return t1;
403
404 code1 = TREE_CODE (t1);
405 code2 = TREE_CODE (t2);
406
407 /* Merge the attributes. */
408 attributes = targetm.merge_type_attributes (t1, t2);
409
410 /* If one is an enumerated type and the other is the compatible
411 integer type, the composite type might be either of the two
412 (DR#013 question 3). For consistency, use the enumerated type as
413 the composite type. */
414
415 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
416 return t1;
417 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
418 return t2;
419
420 gcc_assert (code1 == code2);
421
422 switch (code1)
423 {
424 case POINTER_TYPE:
425 /* For two pointers, do this recursively on the target type. */
426 {
427 tree pointed_to_1 = TREE_TYPE (t1);
428 tree pointed_to_2 = TREE_TYPE (t2);
429 tree target = composite_type (pointed_to_1, pointed_to_2);
430 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
431 t1 = build_type_attribute_variant (t1, attributes);
432 return qualify_type (t1, t2);
433 }
434
435 case ARRAY_TYPE:
436 {
437 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
438 int quals;
439 tree unqual_elt;
440 tree d1 = TYPE_DOMAIN (t1);
441 tree d2 = TYPE_DOMAIN (t2);
442 bool d1_variable, d2_variable;
443 bool d1_zero, d2_zero;
444 bool t1_complete, t2_complete;
445
446 /* We should not have any type quals on arrays at all. */
447 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
448 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
449
450 t1_complete = COMPLETE_TYPE_P (t1);
451 t2_complete = COMPLETE_TYPE_P (t2);
452
453 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
454 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
455
456 d1_variable = (!d1_zero
457 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
458 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
459 d2_variable = (!d2_zero
460 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
461 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
462 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
463 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
464
465 /* Save space: see if the result is identical to one of the args. */
466 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
467 && (d2_variable || d2_zero || !d1_variable))
468 return build_type_attribute_variant (t1, attributes);
469 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
470 && (d1_variable || d1_zero || !d2_variable))
471 return build_type_attribute_variant (t2, attributes);
472
473 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
474 return build_type_attribute_variant (t1, attributes);
475 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
476 return build_type_attribute_variant (t2, attributes);
477
478 /* Merge the element types, and have a size if either arg has
479 one. We may have qualifiers on the element types. To set
480 up TYPE_MAIN_VARIANT correctly, we need to form the
481 composite of the unqualified types and add the qualifiers
482 back at the end. */
483 quals = TYPE_QUALS (strip_array_types (elt));
484 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
485 t1 = build_array_type (unqual_elt,
486 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
487 && (d2_variable
488 || d2_zero
489 || !d1_variable))
490 ? t1
491 : t2));
492 /* Ensure a composite type involving a zero-length array type
493 is a zero-length type not an incomplete type. */
494 if (d1_zero && d2_zero
495 && (t1_complete || t2_complete)
496 && !COMPLETE_TYPE_P (t1))
497 {
498 TYPE_SIZE (t1) = bitsize_zero_node;
499 TYPE_SIZE_UNIT (t1) = size_zero_node;
500 }
501 t1 = c_build_qualified_type (t1, quals);
502 return build_type_attribute_variant (t1, attributes);
503 }
504
505 case ENUMERAL_TYPE:
506 case RECORD_TYPE:
507 case UNION_TYPE:
508 if (attributes != NULL)
509 {
510 /* Try harder not to create a new aggregate type. */
511 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
512 return t1;
513 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
514 return t2;
515 }
516 return build_type_attribute_variant (t1, attributes);
517
518 case FUNCTION_TYPE:
519 /* Function types: prefer the one that specified arg types.
520 If both do, merge the arg types. Also merge the return types. */
521 {
522 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
523 tree p1 = TYPE_ARG_TYPES (t1);
524 tree p2 = TYPE_ARG_TYPES (t2);
525 int len;
526 tree newargs, n;
527 int i;
528
529 /* Save space: see if the result is identical to one of the args. */
530 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
531 return build_type_attribute_variant (t1, attributes);
532 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
533 return build_type_attribute_variant (t2, attributes);
534
535 /* Simple way if one arg fails to specify argument types. */
536 if (TYPE_ARG_TYPES (t1) == 0)
537 {
538 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
539 t1 = build_type_attribute_variant (t1, attributes);
540 return qualify_type (t1, t2);
541 }
542 if (TYPE_ARG_TYPES (t2) == 0)
543 {
544 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
545 t1 = build_type_attribute_variant (t1, attributes);
546 return qualify_type (t1, t2);
547 }
548
549 /* If both args specify argument types, we must merge the two
550 lists, argument by argument. */
551
552 len = list_length (p1);
553 newargs = 0;
554
555 for (i = 0; i < len; i++)
556 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
557
558 n = newargs;
559
560 for (; p1;
561 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
562 {
563 /* A null type means arg type is not specified.
564 Take whatever the other function type has. */
565 if (TREE_VALUE (p1) == 0)
566 {
567 TREE_VALUE (n) = TREE_VALUE (p2);
568 goto parm_done;
569 }
570 if (TREE_VALUE (p2) == 0)
571 {
572 TREE_VALUE (n) = TREE_VALUE (p1);
573 goto parm_done;
574 }
575
576 /* Given wait (union {union wait *u; int *i} *)
577 and wait (union wait *),
578 prefer union wait * as type of parm. */
579 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
580 && TREE_VALUE (p1) != TREE_VALUE (p2))
581 {
582 tree memb;
583 tree mv2 = TREE_VALUE (p2);
584 if (mv2 && mv2 != error_mark_node
585 && TREE_CODE (mv2) != ARRAY_TYPE)
586 mv2 = TYPE_MAIN_VARIANT (mv2);
587 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
588 memb; memb = DECL_CHAIN (memb))
589 {
590 tree mv3 = TREE_TYPE (memb);
591 if (mv3 && mv3 != error_mark_node
592 && TREE_CODE (mv3) != ARRAY_TYPE)
593 mv3 = TYPE_MAIN_VARIANT (mv3);
594 if (comptypes (mv3, mv2))
595 {
596 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
597 TREE_VALUE (p2));
598 pedwarn (input_location, OPT_Wpedantic,
599 "function types not truly compatible in ISO C");
600 goto parm_done;
601 }
602 }
603 }
604 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
605 && TREE_VALUE (p2) != TREE_VALUE (p1))
606 {
607 tree memb;
608 tree mv1 = TREE_VALUE (p1);
609 if (mv1 && mv1 != error_mark_node
610 && TREE_CODE (mv1) != ARRAY_TYPE)
611 mv1 = TYPE_MAIN_VARIANT (mv1);
612 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
613 memb; memb = DECL_CHAIN (memb))
614 {
615 tree mv3 = TREE_TYPE (memb);
616 if (mv3 && mv3 != error_mark_node
617 && TREE_CODE (mv3) != ARRAY_TYPE)
618 mv3 = TYPE_MAIN_VARIANT (mv3);
619 if (comptypes (mv3, mv1))
620 {
621 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
622 TREE_VALUE (p1));
623 pedwarn (input_location, OPT_Wpedantic,
624 "function types not truly compatible in ISO C");
625 goto parm_done;
626 }
627 }
628 }
629 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
630 parm_done: ;
631 }
632
633 t1 = build_function_type (valtype, newargs);
634 t1 = qualify_type (t1, t2);
635 /* ... falls through ... */
636 }
637
638 default:
639 return build_type_attribute_variant (t1, attributes);
640 }
641
642 }
643
644 /* Return the type of a conditional expression between pointers to
645 possibly differently qualified versions of compatible types.
646
647 We assume that comp_target_types has already been done and returned
648 nonzero; if that isn't so, this may crash. */
649
650 static tree
651 common_pointer_type (tree t1, tree t2)
652 {
653 tree attributes;
654 tree pointed_to_1, mv1;
655 tree pointed_to_2, mv2;
656 tree target;
657 unsigned target_quals;
658 addr_space_t as1, as2, as_common;
659 int quals1, quals2;
660
661 /* Save time if the two types are the same. */
662
663 if (t1 == t2) return t1;
664
665 /* If one type is nonsense, use the other. */
666 if (t1 == error_mark_node)
667 return t2;
668 if (t2 == error_mark_node)
669 return t1;
670
671 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
672 && TREE_CODE (t2) == POINTER_TYPE);
673
674 /* Merge the attributes. */
675 attributes = targetm.merge_type_attributes (t1, t2);
676
677 /* Find the composite type of the target types, and combine the
678 qualifiers of the two types' targets. Do not lose qualifiers on
679 array element types by taking the TYPE_MAIN_VARIANT. */
680 mv1 = pointed_to_1 = TREE_TYPE (t1);
681 mv2 = pointed_to_2 = TREE_TYPE (t2);
682 if (TREE_CODE (mv1) != ARRAY_TYPE)
683 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
684 if (TREE_CODE (mv2) != ARRAY_TYPE)
685 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
686 target = composite_type (mv1, mv2);
687
688 /* Strip array types to get correct qualifier for pointers to arrays */
689 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
690 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
691
692 /* For function types do not merge const qualifiers, but drop them
693 if used inconsistently. The middle-end uses these to mark const
694 and noreturn functions. */
695 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
696 target_quals = (quals1 & quals2);
697 else
698 target_quals = (quals1 | quals2);
699
700 /* If the two named address spaces are different, determine the common
701 superset address space. This is guaranteed to exist due to the
702 assumption that comp_target_type returned non-zero. */
703 as1 = TYPE_ADDR_SPACE (pointed_to_1);
704 as2 = TYPE_ADDR_SPACE (pointed_to_2);
705 if (!addr_space_superset (as1, as2, &as_common))
706 gcc_unreachable ();
707
708 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
709
710 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
711 return build_type_attribute_variant (t1, attributes);
712 }
713
714 /* Return the common type for two arithmetic types under the usual
715 arithmetic conversions. The default conversions have already been
716 applied, and enumerated types converted to their compatible integer
717 types. The resulting type is unqualified and has no attributes.
718
719 This is the type for the result of most arithmetic operations
720 if the operands have the given two types. */
721
722 static tree
723 c_common_type (tree t1, tree t2)
724 {
725 enum tree_code code1;
726 enum tree_code code2;
727
728 /* If one type is nonsense, use the other. */
729 if (t1 == error_mark_node)
730 return t2;
731 if (t2 == error_mark_node)
732 return t1;
733
734 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
735 t1 = TYPE_MAIN_VARIANT (t1);
736
737 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
738 t2 = TYPE_MAIN_VARIANT (t2);
739
740 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
741 t1 = build_type_attribute_variant (t1, NULL_TREE);
742
743 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
744 t2 = build_type_attribute_variant (t2, NULL_TREE);
745
746 /* Save time if the two types are the same. */
747
748 if (t1 == t2) return t1;
749
750 code1 = TREE_CODE (t1);
751 code2 = TREE_CODE (t2);
752
753 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
754 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
755 || code1 == INTEGER_TYPE);
756 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
757 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
758 || code2 == INTEGER_TYPE);
759
760 /* When one operand is a decimal float type, the other operand cannot be
761 a generic float type or a complex type. We also disallow vector types
762 here. */
763 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
764 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
765 {
766 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
767 {
768 error ("can%'t mix operands of decimal float and vector types");
769 return error_mark_node;
770 }
771 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
772 {
773 error ("can%'t mix operands of decimal float and complex types");
774 return error_mark_node;
775 }
776 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
777 {
778 error ("can%'t mix operands of decimal float and other float types");
779 return error_mark_node;
780 }
781 }
782
783 /* If one type is a vector type, return that type. (How the usual
784 arithmetic conversions apply to the vector types extension is not
785 precisely specified.) */
786 if (code1 == VECTOR_TYPE)
787 return t1;
788
789 if (code2 == VECTOR_TYPE)
790 return t2;
791
792 /* If one type is complex, form the common type of the non-complex
793 components, then make that complex. Use T1 or T2 if it is the
794 required type. */
795 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
796 {
797 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
798 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
799 tree subtype = c_common_type (subtype1, subtype2);
800
801 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
802 return t1;
803 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
804 return t2;
805 else
806 return build_complex_type (subtype);
807 }
808
809 /* If only one is real, use it as the result. */
810
811 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
812 return t1;
813
814 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
815 return t2;
816
817 /* If both are real and either are decimal floating point types, use
818 the decimal floating point type with the greater precision. */
819
820 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
821 {
822 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
823 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
824 return dfloat128_type_node;
825 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
826 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
827 return dfloat64_type_node;
828 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
829 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
830 return dfloat32_type_node;
831 }
832
833 /* Deal with fixed-point types. */
834 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
835 {
836 unsigned int unsignedp = 0, satp = 0;
837 machine_mode m1, m2;
838 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
839
840 m1 = TYPE_MODE (t1);
841 m2 = TYPE_MODE (t2);
842
843 /* If one input type is saturating, the result type is saturating. */
844 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
845 satp = 1;
846
847 /* If both fixed-point types are unsigned, the result type is unsigned.
848 When mixing fixed-point and integer types, follow the sign of the
849 fixed-point type.
850 Otherwise, the result type is signed. */
851 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
852 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
853 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
854 && TYPE_UNSIGNED (t1))
855 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
856 && TYPE_UNSIGNED (t2)))
857 unsignedp = 1;
858
859 /* The result type is signed. */
860 if (unsignedp == 0)
861 {
862 /* If the input type is unsigned, we need to convert to the
863 signed type. */
864 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
865 {
866 enum mode_class mclass = (enum mode_class) 0;
867 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
868 mclass = MODE_FRACT;
869 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
870 mclass = MODE_ACCUM;
871 else
872 gcc_unreachable ();
873 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
874 }
875 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
876 {
877 enum mode_class mclass = (enum mode_class) 0;
878 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
879 mclass = MODE_FRACT;
880 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
881 mclass = MODE_ACCUM;
882 else
883 gcc_unreachable ();
884 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
885 }
886 }
887
888 if (code1 == FIXED_POINT_TYPE)
889 {
890 fbit1 = GET_MODE_FBIT (m1);
891 ibit1 = GET_MODE_IBIT (m1);
892 }
893 else
894 {
895 fbit1 = 0;
896 /* Signed integers need to subtract one sign bit. */
897 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
898 }
899
900 if (code2 == FIXED_POINT_TYPE)
901 {
902 fbit2 = GET_MODE_FBIT (m2);
903 ibit2 = GET_MODE_IBIT (m2);
904 }
905 else
906 {
907 fbit2 = 0;
908 /* Signed integers need to subtract one sign bit. */
909 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
910 }
911
912 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
913 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
914 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
915 satp);
916 }
917
918 /* Both real or both integers; use the one with greater precision. */
919
920 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
921 return t1;
922 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
923 return t2;
924
925 /* Same precision. Prefer long longs to longs to ints when the
926 same precision, following the C99 rules on integer type rank
927 (which are equivalent to the C90 rules for C90 types). */
928
929 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
930 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
931 return long_long_unsigned_type_node;
932
933 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
934 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
935 {
936 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
937 return long_long_unsigned_type_node;
938 else
939 return long_long_integer_type_node;
940 }
941
942 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
943 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
944 return long_unsigned_type_node;
945
946 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
947 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
948 {
949 /* But preserve unsignedness from the other type,
950 since long cannot hold all the values of an unsigned int. */
951 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
952 return long_unsigned_type_node;
953 else
954 return long_integer_type_node;
955 }
956
957 /* Likewise, prefer long double to double even if same size. */
958 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
959 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
960 return long_double_type_node;
961
962 /* Likewise, prefer double to float even if same size.
963 We got a couple of embedded targets with 32 bit doubles, and the
964 pdp11 might have 64 bit floats. */
965 if (TYPE_MAIN_VARIANT (t1) == double_type_node
966 || TYPE_MAIN_VARIANT (t2) == double_type_node)
967 return double_type_node;
968
969 /* Otherwise prefer the unsigned one. */
970
971 if (TYPE_UNSIGNED (t1))
972 return t1;
973 else
974 return t2;
975 }
976 \f
977 /* Wrapper around c_common_type that is used by c-common.c and other
978 front end optimizations that remove promotions. ENUMERAL_TYPEs
979 are allowed here and are converted to their compatible integer types.
980 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
981 preferably a non-Boolean type as the common type. */
982 tree
983 common_type (tree t1, tree t2)
984 {
985 if (TREE_CODE (t1) == ENUMERAL_TYPE)
986 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
987 if (TREE_CODE (t2) == ENUMERAL_TYPE)
988 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
989
990 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
991 if (TREE_CODE (t1) == BOOLEAN_TYPE
992 && TREE_CODE (t2) == BOOLEAN_TYPE)
993 return boolean_type_node;
994
995 /* If either type is BOOLEAN_TYPE, then return the other. */
996 if (TREE_CODE (t1) == BOOLEAN_TYPE)
997 return t2;
998 if (TREE_CODE (t2) == BOOLEAN_TYPE)
999 return t1;
1000
1001 return c_common_type (t1, t2);
1002 }
1003
1004 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1005 or various other operations. Return 2 if they are compatible
1006 but a warning may be needed if you use them together. */
1007
1008 int
1009 comptypes (tree type1, tree type2)
1010 {
1011 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1012 int val;
1013
1014 val = comptypes_internal (type1, type2, NULL, NULL);
1015 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1016
1017 return val;
1018 }
1019
1020 /* Like comptypes, but if it returns non-zero because enum and int are
1021 compatible, it sets *ENUM_AND_INT_P to true. */
1022
1023 static int
1024 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1025 {
1026 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1027 int val;
1028
1029 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1030 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1031
1032 return val;
1033 }
1034
1035 /* Like comptypes, but if it returns nonzero for different types, it
1036 sets *DIFFERENT_TYPES_P to true. */
1037
1038 int
1039 comptypes_check_different_types (tree type1, tree type2,
1040 bool *different_types_p)
1041 {
1042 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1043 int val;
1044
1045 val = comptypes_internal (type1, type2, NULL, different_types_p);
1046 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1047
1048 return val;
1049 }
1050 \f
1051 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1052 or various other operations. Return 2 if they are compatible
1053 but a warning may be needed if you use them together. If
1054 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1055 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1056 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1057 NULL, and the types are compatible but different enough not to be
1058 permitted in C11 typedef redeclarations, then this sets
1059 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1060 false, but may or may not be set if the types are incompatible.
1061 This differs from comptypes, in that we don't free the seen
1062 types. */
1063
1064 static int
1065 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1066 bool *different_types_p)
1067 {
1068 const_tree t1 = type1;
1069 const_tree t2 = type2;
1070 int attrval, val;
1071
1072 /* Suppress errors caused by previously reported errors. */
1073
1074 if (t1 == t2 || !t1 || !t2
1075 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1076 return 1;
1077
1078 /* Enumerated types are compatible with integer types, but this is
1079 not transitive: two enumerated types in the same translation unit
1080 are compatible with each other only if they are the same type. */
1081
1082 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1083 {
1084 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1085 if (TREE_CODE (t2) != VOID_TYPE)
1086 {
1087 if (enum_and_int_p != NULL)
1088 *enum_and_int_p = true;
1089 if (different_types_p != NULL)
1090 *different_types_p = true;
1091 }
1092 }
1093 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1094 {
1095 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1096 if (TREE_CODE (t1) != VOID_TYPE)
1097 {
1098 if (enum_and_int_p != NULL)
1099 *enum_and_int_p = true;
1100 if (different_types_p != NULL)
1101 *different_types_p = true;
1102 }
1103 }
1104
1105 if (t1 == t2)
1106 return 1;
1107
1108 /* Different classes of types can't be compatible. */
1109
1110 if (TREE_CODE (t1) != TREE_CODE (t2))
1111 return 0;
1112
1113 /* Qualifiers must match. C99 6.7.3p9 */
1114
1115 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1116 return 0;
1117
1118 /* Allow for two different type nodes which have essentially the same
1119 definition. Note that we already checked for equality of the type
1120 qualifiers (just above). */
1121
1122 if (TREE_CODE (t1) != ARRAY_TYPE
1123 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1124 return 1;
1125
1126 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1127 if (!(attrval = comp_type_attributes (t1, t2)))
1128 return 0;
1129
1130 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1131 val = 0;
1132
1133 switch (TREE_CODE (t1))
1134 {
1135 case POINTER_TYPE:
1136 /* Do not remove mode or aliasing information. */
1137 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1138 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1139 break;
1140 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1141 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1142 enum_and_int_p, different_types_p));
1143 break;
1144
1145 case FUNCTION_TYPE:
1146 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1147 different_types_p);
1148 break;
1149
1150 case ARRAY_TYPE:
1151 {
1152 tree d1 = TYPE_DOMAIN (t1);
1153 tree d2 = TYPE_DOMAIN (t2);
1154 bool d1_variable, d2_variable;
1155 bool d1_zero, d2_zero;
1156 val = 1;
1157
1158 /* Target types must match incl. qualifiers. */
1159 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1160 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1161 enum_and_int_p,
1162 different_types_p)))
1163 return 0;
1164
1165 if (different_types_p != NULL
1166 && (d1 == 0) != (d2 == 0))
1167 *different_types_p = true;
1168 /* Sizes must match unless one is missing or variable. */
1169 if (d1 == 0 || d2 == 0 || d1 == d2)
1170 break;
1171
1172 d1_zero = !TYPE_MAX_VALUE (d1);
1173 d2_zero = !TYPE_MAX_VALUE (d2);
1174
1175 d1_variable = (!d1_zero
1176 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1177 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1178 d2_variable = (!d2_zero
1179 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1180 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1181 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1182 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1183
1184 if (different_types_p != NULL
1185 && d1_variable != d2_variable)
1186 *different_types_p = true;
1187 if (d1_variable || d2_variable)
1188 break;
1189 if (d1_zero && d2_zero)
1190 break;
1191 if (d1_zero || d2_zero
1192 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1193 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1194 val = 0;
1195
1196 break;
1197 }
1198
1199 case ENUMERAL_TYPE:
1200 case RECORD_TYPE:
1201 case UNION_TYPE:
1202 if (val != 1 && !same_translation_unit_p (t1, t2))
1203 {
1204 tree a1 = TYPE_ATTRIBUTES (t1);
1205 tree a2 = TYPE_ATTRIBUTES (t2);
1206
1207 if (! attribute_list_contained (a1, a2)
1208 && ! attribute_list_contained (a2, a1))
1209 break;
1210
1211 if (attrval != 2)
1212 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1213 different_types_p);
1214 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1215 different_types_p);
1216 }
1217 break;
1218
1219 case VECTOR_TYPE:
1220 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1221 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1222 enum_and_int_p, different_types_p));
1223 break;
1224
1225 default:
1226 break;
1227 }
1228 return attrval == 2 && val == 1 ? 2 : val;
1229 }
1230
1231 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1232 their qualifiers, except for named address spaces. If the pointers point to
1233 different named addresses, then we must determine if one address space is a
1234 subset of the other. */
1235
1236 static int
1237 comp_target_types (location_t location, tree ttl, tree ttr)
1238 {
1239 int val;
1240 int val_ped;
1241 tree mvl = TREE_TYPE (ttl);
1242 tree mvr = TREE_TYPE (ttr);
1243 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1244 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1245 addr_space_t as_common;
1246 bool enum_and_int_p;
1247
1248 /* Fail if pointers point to incompatible address spaces. */
1249 if (!addr_space_superset (asl, asr, &as_common))
1250 return 0;
1251
1252 /* For pedantic record result of comptypes on arrays before losing
1253 qualifiers on the element type below. */
1254 val_ped = 1;
1255
1256 if (TREE_CODE (mvl) == ARRAY_TYPE
1257 && TREE_CODE (mvr) == ARRAY_TYPE)
1258 val_ped = comptypes (mvl, mvr);
1259
1260 /* Qualifiers on element types of array types that are
1261 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1262
1263 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1264 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1265 : TYPE_MAIN_VARIANT (mvl));
1266
1267 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1268 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1269 : TYPE_MAIN_VARIANT (mvr));
1270
1271 enum_and_int_p = false;
1272 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1273
1274 if (val == 1 && val_ped != 1)
1275 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1276 "are incompatible in ISO C");
1277
1278 if (val == 2)
1279 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1280
1281 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1282 warning_at (location, OPT_Wc___compat,
1283 "pointer target types incompatible in C++");
1284
1285 return val;
1286 }
1287 \f
1288 /* Subroutines of `comptypes'. */
1289
1290 /* Determine whether two trees derive from the same translation unit.
1291 If the CONTEXT chain ends in a null, that tree's context is still
1292 being parsed, so if two trees have context chains ending in null,
1293 they're in the same translation unit. */
1294 int
1295 same_translation_unit_p (const_tree t1, const_tree t2)
1296 {
1297 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1298 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1299 {
1300 case tcc_declaration:
1301 t1 = DECL_CONTEXT (t1); break;
1302 case tcc_type:
1303 t1 = TYPE_CONTEXT (t1); break;
1304 case tcc_exceptional:
1305 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1306 default: gcc_unreachable ();
1307 }
1308
1309 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1310 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1311 {
1312 case tcc_declaration:
1313 t2 = DECL_CONTEXT (t2); break;
1314 case tcc_type:
1315 t2 = TYPE_CONTEXT (t2); break;
1316 case tcc_exceptional:
1317 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1318 default: gcc_unreachable ();
1319 }
1320
1321 return t1 == t2;
1322 }
1323
1324 /* Allocate the seen two types, assuming that they are compatible. */
1325
1326 static struct tagged_tu_seen_cache *
1327 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1328 {
1329 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1330 tu->next = tagged_tu_seen_base;
1331 tu->t1 = t1;
1332 tu->t2 = t2;
1333
1334 tagged_tu_seen_base = tu;
1335
1336 /* The C standard says that two structures in different translation
1337 units are compatible with each other only if the types of their
1338 fields are compatible (among other things). We assume that they
1339 are compatible until proven otherwise when building the cache.
1340 An example where this can occur is:
1341 struct a
1342 {
1343 struct a *next;
1344 };
1345 If we are comparing this against a similar struct in another TU,
1346 and did not assume they were compatible, we end up with an infinite
1347 loop. */
1348 tu->val = 1;
1349 return tu;
1350 }
1351
1352 /* Free the seen types until we get to TU_TIL. */
1353
1354 static void
1355 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1356 {
1357 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1358 while (tu != tu_til)
1359 {
1360 const struct tagged_tu_seen_cache *const tu1
1361 = (const struct tagged_tu_seen_cache *) tu;
1362 tu = tu1->next;
1363 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1364 }
1365 tagged_tu_seen_base = tu_til;
1366 }
1367
1368 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1369 compatible. If the two types are not the same (which has been
1370 checked earlier), this can only happen when multiple translation
1371 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1372 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1373 comptypes_internal. */
1374
1375 static int
1376 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1377 bool *enum_and_int_p, bool *different_types_p)
1378 {
1379 tree s1, s2;
1380 bool needs_warning = false;
1381
1382 /* We have to verify that the tags of the types are the same. This
1383 is harder than it looks because this may be a typedef, so we have
1384 to go look at the original type. It may even be a typedef of a
1385 typedef...
1386 In the case of compiler-created builtin structs the TYPE_DECL
1387 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1388 while (TYPE_NAME (t1)
1389 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1390 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1391 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1392
1393 while (TYPE_NAME (t2)
1394 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1395 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1396 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1397
1398 /* C90 didn't have the requirement that the two tags be the same. */
1399 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1400 return 0;
1401
1402 /* C90 didn't say what happened if one or both of the types were
1403 incomplete; we choose to follow C99 rules here, which is that they
1404 are compatible. */
1405 if (TYPE_SIZE (t1) == NULL
1406 || TYPE_SIZE (t2) == NULL)
1407 return 1;
1408
1409 {
1410 const struct tagged_tu_seen_cache * tts_i;
1411 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1412 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1413 return tts_i->val;
1414 }
1415
1416 switch (TREE_CODE (t1))
1417 {
1418 case ENUMERAL_TYPE:
1419 {
1420 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1421 /* Speed up the case where the type values are in the same order. */
1422 tree tv1 = TYPE_VALUES (t1);
1423 tree tv2 = TYPE_VALUES (t2);
1424
1425 if (tv1 == tv2)
1426 {
1427 return 1;
1428 }
1429
1430 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1431 {
1432 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1433 break;
1434 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1435 {
1436 tu->val = 0;
1437 return 0;
1438 }
1439 }
1440
1441 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1442 {
1443 return 1;
1444 }
1445 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1446 {
1447 tu->val = 0;
1448 return 0;
1449 }
1450
1451 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1452 {
1453 tu->val = 0;
1454 return 0;
1455 }
1456
1457 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1458 {
1459 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1460 if (s2 == NULL
1461 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1462 {
1463 tu->val = 0;
1464 return 0;
1465 }
1466 }
1467 return 1;
1468 }
1469
1470 case UNION_TYPE:
1471 {
1472 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1473 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1474 {
1475 tu->val = 0;
1476 return 0;
1477 }
1478
1479 /* Speed up the common case where the fields are in the same order. */
1480 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1481 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1482 {
1483 int result;
1484
1485 if (DECL_NAME (s1) != DECL_NAME (s2))
1486 break;
1487 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1488 enum_and_int_p, different_types_p);
1489
1490 if (result != 1 && !DECL_NAME (s1))
1491 break;
1492 if (result == 0)
1493 {
1494 tu->val = 0;
1495 return 0;
1496 }
1497 if (result == 2)
1498 needs_warning = true;
1499
1500 if (TREE_CODE (s1) == FIELD_DECL
1501 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1502 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1503 {
1504 tu->val = 0;
1505 return 0;
1506 }
1507 }
1508 if (!s1 && !s2)
1509 {
1510 tu->val = needs_warning ? 2 : 1;
1511 return tu->val;
1512 }
1513
1514 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1515 {
1516 bool ok = false;
1517
1518 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1519 if (DECL_NAME (s1) == DECL_NAME (s2))
1520 {
1521 int result;
1522
1523 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1524 enum_and_int_p,
1525 different_types_p);
1526
1527 if (result != 1 && !DECL_NAME (s1))
1528 continue;
1529 if (result == 0)
1530 {
1531 tu->val = 0;
1532 return 0;
1533 }
1534 if (result == 2)
1535 needs_warning = true;
1536
1537 if (TREE_CODE (s1) == FIELD_DECL
1538 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1539 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1540 break;
1541
1542 ok = true;
1543 break;
1544 }
1545 if (!ok)
1546 {
1547 tu->val = 0;
1548 return 0;
1549 }
1550 }
1551 tu->val = needs_warning ? 2 : 10;
1552 return tu->val;
1553 }
1554
1555 case RECORD_TYPE:
1556 {
1557 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1558
1559 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1560 s1 && s2;
1561 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1562 {
1563 int result;
1564 if (TREE_CODE (s1) != TREE_CODE (s2)
1565 || DECL_NAME (s1) != DECL_NAME (s2))
1566 break;
1567 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1568 enum_and_int_p, different_types_p);
1569 if (result == 0)
1570 break;
1571 if (result == 2)
1572 needs_warning = true;
1573
1574 if (TREE_CODE (s1) == FIELD_DECL
1575 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1576 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1577 break;
1578 }
1579 if (s1 && s2)
1580 tu->val = 0;
1581 else
1582 tu->val = needs_warning ? 2 : 1;
1583 return tu->val;
1584 }
1585
1586 default:
1587 gcc_unreachable ();
1588 }
1589 }
1590
1591 /* Return 1 if two function types F1 and F2 are compatible.
1592 If either type specifies no argument types,
1593 the other must specify a fixed number of self-promoting arg types.
1594 Otherwise, if one type specifies only the number of arguments,
1595 the other must specify that number of self-promoting arg types.
1596 Otherwise, the argument types must match.
1597 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1598
1599 static int
1600 function_types_compatible_p (const_tree f1, const_tree f2,
1601 bool *enum_and_int_p, bool *different_types_p)
1602 {
1603 tree args1, args2;
1604 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1605 int val = 1;
1606 int val1;
1607 tree ret1, ret2;
1608
1609 ret1 = TREE_TYPE (f1);
1610 ret2 = TREE_TYPE (f2);
1611
1612 /* 'volatile' qualifiers on a function's return type used to mean
1613 the function is noreturn. */
1614 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1615 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1616 if (TYPE_VOLATILE (ret1))
1617 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1618 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1619 if (TYPE_VOLATILE (ret2))
1620 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1621 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1622 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1623 if (val == 0)
1624 return 0;
1625
1626 args1 = TYPE_ARG_TYPES (f1);
1627 args2 = TYPE_ARG_TYPES (f2);
1628
1629 if (different_types_p != NULL
1630 && (args1 == 0) != (args2 == 0))
1631 *different_types_p = true;
1632
1633 /* An unspecified parmlist matches any specified parmlist
1634 whose argument types don't need default promotions. */
1635
1636 if (args1 == 0)
1637 {
1638 if (!self_promoting_args_p (args2))
1639 return 0;
1640 /* If one of these types comes from a non-prototype fn definition,
1641 compare that with the other type's arglist.
1642 If they don't match, ask for a warning (but no error). */
1643 if (TYPE_ACTUAL_ARG_TYPES (f1)
1644 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1645 enum_and_int_p, different_types_p))
1646 val = 2;
1647 return val;
1648 }
1649 if (args2 == 0)
1650 {
1651 if (!self_promoting_args_p (args1))
1652 return 0;
1653 if (TYPE_ACTUAL_ARG_TYPES (f2)
1654 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1655 enum_and_int_p, different_types_p))
1656 val = 2;
1657 return val;
1658 }
1659
1660 /* Both types have argument lists: compare them and propagate results. */
1661 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1662 different_types_p);
1663 return val1 != 1 ? val1 : val;
1664 }
1665
1666 /* Check two lists of types for compatibility, returning 0 for
1667 incompatible, 1 for compatible, or 2 for compatible with
1668 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1669 comptypes_internal. */
1670
1671 static int
1672 type_lists_compatible_p (const_tree args1, const_tree args2,
1673 bool *enum_and_int_p, bool *different_types_p)
1674 {
1675 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1676 int val = 1;
1677 int newval = 0;
1678
1679 while (1)
1680 {
1681 tree a1, mv1, a2, mv2;
1682 if (args1 == 0 && args2 == 0)
1683 return val;
1684 /* If one list is shorter than the other,
1685 they fail to match. */
1686 if (args1 == 0 || args2 == 0)
1687 return 0;
1688 mv1 = a1 = TREE_VALUE (args1);
1689 mv2 = a2 = TREE_VALUE (args2);
1690 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1691 mv1 = (TYPE_ATOMIC (mv1)
1692 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1693 TYPE_QUAL_ATOMIC)
1694 : TYPE_MAIN_VARIANT (mv1));
1695 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1696 mv2 = (TYPE_ATOMIC (mv2)
1697 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1698 TYPE_QUAL_ATOMIC)
1699 : TYPE_MAIN_VARIANT (mv2));
1700 /* A null pointer instead of a type
1701 means there is supposed to be an argument
1702 but nothing is specified about what type it has.
1703 So match anything that self-promotes. */
1704 if (different_types_p != NULL
1705 && (a1 == 0) != (a2 == 0))
1706 *different_types_p = true;
1707 if (a1 == 0)
1708 {
1709 if (c_type_promotes_to (a2) != a2)
1710 return 0;
1711 }
1712 else if (a2 == 0)
1713 {
1714 if (c_type_promotes_to (a1) != a1)
1715 return 0;
1716 }
1717 /* If one of the lists has an error marker, ignore this arg. */
1718 else if (TREE_CODE (a1) == ERROR_MARK
1719 || TREE_CODE (a2) == ERROR_MARK)
1720 ;
1721 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1722 different_types_p)))
1723 {
1724 if (different_types_p != NULL)
1725 *different_types_p = true;
1726 /* Allow wait (union {union wait *u; int *i} *)
1727 and wait (union wait *) to be compatible. */
1728 if (TREE_CODE (a1) == UNION_TYPE
1729 && (TYPE_NAME (a1) == 0
1730 || TYPE_TRANSPARENT_AGGR (a1))
1731 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1732 && tree_int_cst_equal (TYPE_SIZE (a1),
1733 TYPE_SIZE (a2)))
1734 {
1735 tree memb;
1736 for (memb = TYPE_FIELDS (a1);
1737 memb; memb = DECL_CHAIN (memb))
1738 {
1739 tree mv3 = TREE_TYPE (memb);
1740 if (mv3 && mv3 != error_mark_node
1741 && TREE_CODE (mv3) != ARRAY_TYPE)
1742 mv3 = (TYPE_ATOMIC (mv3)
1743 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1744 TYPE_QUAL_ATOMIC)
1745 : TYPE_MAIN_VARIANT (mv3));
1746 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1747 different_types_p))
1748 break;
1749 }
1750 if (memb == 0)
1751 return 0;
1752 }
1753 else if (TREE_CODE (a2) == UNION_TYPE
1754 && (TYPE_NAME (a2) == 0
1755 || TYPE_TRANSPARENT_AGGR (a2))
1756 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1757 && tree_int_cst_equal (TYPE_SIZE (a2),
1758 TYPE_SIZE (a1)))
1759 {
1760 tree memb;
1761 for (memb = TYPE_FIELDS (a2);
1762 memb; memb = DECL_CHAIN (memb))
1763 {
1764 tree mv3 = TREE_TYPE (memb);
1765 if (mv3 && mv3 != error_mark_node
1766 && TREE_CODE (mv3) != ARRAY_TYPE)
1767 mv3 = (TYPE_ATOMIC (mv3)
1768 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1769 TYPE_QUAL_ATOMIC)
1770 : TYPE_MAIN_VARIANT (mv3));
1771 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1772 different_types_p))
1773 break;
1774 }
1775 if (memb == 0)
1776 return 0;
1777 }
1778 else
1779 return 0;
1780 }
1781
1782 /* comptypes said ok, but record if it said to warn. */
1783 if (newval > val)
1784 val = newval;
1785
1786 args1 = TREE_CHAIN (args1);
1787 args2 = TREE_CHAIN (args2);
1788 }
1789 }
1790 \f
1791 /* Compute the size to increment a pointer by. When a function type or void
1792 type or incomplete type is passed, size_one_node is returned.
1793 This function does not emit any diagnostics; the caller is responsible
1794 for that. */
1795
1796 static tree
1797 c_size_in_bytes (const_tree type)
1798 {
1799 enum tree_code code = TREE_CODE (type);
1800
1801 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1802 || !COMPLETE_TYPE_P (type))
1803 return size_one_node;
1804
1805 /* Convert in case a char is more than one unit. */
1806 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1807 size_int (TYPE_PRECISION (char_type_node)
1808 / BITS_PER_UNIT));
1809 }
1810 \f
1811 /* Return either DECL or its known constant value (if it has one). */
1812
1813 tree
1814 decl_constant_value (tree decl)
1815 {
1816 if (/* Don't change a variable array bound or initial value to a constant
1817 in a place where a variable is invalid. Note that DECL_INITIAL
1818 isn't valid for a PARM_DECL. */
1819 current_function_decl != 0
1820 && TREE_CODE (decl) != PARM_DECL
1821 && !TREE_THIS_VOLATILE (decl)
1822 && TREE_READONLY (decl)
1823 && DECL_INITIAL (decl) != 0
1824 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1825 /* This is invalid if initial value is not constant.
1826 If it has either a function call, a memory reference,
1827 or a variable, then re-evaluating it could give different results. */
1828 && TREE_CONSTANT (DECL_INITIAL (decl))
1829 /* Check for cases where this is sub-optimal, even though valid. */
1830 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1831 return DECL_INITIAL (decl);
1832 return decl;
1833 }
1834
1835 /* Convert the array expression EXP to a pointer. */
1836 static tree
1837 array_to_pointer_conversion (location_t loc, tree exp)
1838 {
1839 tree orig_exp = exp;
1840 tree type = TREE_TYPE (exp);
1841 tree adr;
1842 tree restype = TREE_TYPE (type);
1843 tree ptrtype;
1844
1845 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1846
1847 STRIP_TYPE_NOPS (exp);
1848
1849 if (TREE_NO_WARNING (orig_exp))
1850 TREE_NO_WARNING (exp) = 1;
1851
1852 ptrtype = build_pointer_type (restype);
1853
1854 if (TREE_CODE (exp) == INDIRECT_REF)
1855 return convert (ptrtype, TREE_OPERAND (exp, 0));
1856
1857 /* In C++ array compound literals are temporary objects unless they are
1858 const or appear in namespace scope, so they are destroyed too soon
1859 to use them for much of anything (c++/53220). */
1860 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1861 {
1862 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1863 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1864 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1865 "converting an array compound literal to a pointer "
1866 "is ill-formed in C++");
1867 }
1868
1869 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1870 return convert (ptrtype, adr);
1871 }
1872
1873 /* Convert the function expression EXP to a pointer. */
1874 static tree
1875 function_to_pointer_conversion (location_t loc, tree exp)
1876 {
1877 tree orig_exp = exp;
1878
1879 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1880
1881 STRIP_TYPE_NOPS (exp);
1882
1883 if (TREE_NO_WARNING (orig_exp))
1884 TREE_NO_WARNING (exp) = 1;
1885
1886 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1887 }
1888
1889 /* Mark EXP as read, not just set, for set but not used -Wunused
1890 warning purposes. */
1891
1892 void
1893 mark_exp_read (tree exp)
1894 {
1895 switch (TREE_CODE (exp))
1896 {
1897 case VAR_DECL:
1898 case PARM_DECL:
1899 DECL_READ_P (exp) = 1;
1900 break;
1901 case ARRAY_REF:
1902 case COMPONENT_REF:
1903 case MODIFY_EXPR:
1904 case REALPART_EXPR:
1905 case IMAGPART_EXPR:
1906 CASE_CONVERT:
1907 case ADDR_EXPR:
1908 mark_exp_read (TREE_OPERAND (exp, 0));
1909 break;
1910 case COMPOUND_EXPR:
1911 case C_MAYBE_CONST_EXPR:
1912 mark_exp_read (TREE_OPERAND (exp, 1));
1913 break;
1914 default:
1915 break;
1916 }
1917 }
1918
1919 /* Perform the default conversion of arrays and functions to pointers.
1920 Return the result of converting EXP. For any other expression, just
1921 return EXP.
1922
1923 LOC is the location of the expression. */
1924
1925 struct c_expr
1926 default_function_array_conversion (location_t loc, struct c_expr exp)
1927 {
1928 tree orig_exp = exp.value;
1929 tree type = TREE_TYPE (exp.value);
1930 enum tree_code code = TREE_CODE (type);
1931
1932 switch (code)
1933 {
1934 case ARRAY_TYPE:
1935 {
1936 bool not_lvalue = false;
1937 bool lvalue_array_p;
1938
1939 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1940 || CONVERT_EXPR_P (exp.value))
1941 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1942 {
1943 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1944 not_lvalue = true;
1945 exp.value = TREE_OPERAND (exp.value, 0);
1946 }
1947
1948 if (TREE_NO_WARNING (orig_exp))
1949 TREE_NO_WARNING (exp.value) = 1;
1950
1951 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1952 if (!flag_isoc99 && !lvalue_array_p)
1953 {
1954 /* Before C99, non-lvalue arrays do not decay to pointers.
1955 Normally, using such an array would be invalid; but it can
1956 be used correctly inside sizeof or as a statement expression.
1957 Thus, do not give an error here; an error will result later. */
1958 return exp;
1959 }
1960
1961 exp.value = array_to_pointer_conversion (loc, exp.value);
1962 }
1963 break;
1964 case FUNCTION_TYPE:
1965 exp.value = function_to_pointer_conversion (loc, exp.value);
1966 break;
1967 default:
1968 break;
1969 }
1970
1971 return exp;
1972 }
1973
1974 struct c_expr
1975 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1976 {
1977 mark_exp_read (exp.value);
1978 return default_function_array_conversion (loc, exp);
1979 }
1980
1981 /* Return whether EXPR should be treated as an atomic lvalue for the
1982 purposes of load and store handling. */
1983
1984 static bool
1985 really_atomic_lvalue (tree expr)
1986 {
1987 if (error_operand_p (expr))
1988 return false;
1989 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1990 return false;
1991 if (!lvalue_p (expr))
1992 return false;
1993
1994 /* Ignore _Atomic on register variables, since their addresses can't
1995 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1996 sequences wouldn't work. Ignore _Atomic on structures containing
1997 bit-fields, since accessing elements of atomic structures or
1998 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1999 it's undefined at translation time or execution time, and the
2000 normal atomic sequences again wouldn't work. */
2001 while (handled_component_p (expr))
2002 {
2003 if (TREE_CODE (expr) == COMPONENT_REF
2004 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2005 return false;
2006 expr = TREE_OPERAND (expr, 0);
2007 }
2008 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2009 return false;
2010 return true;
2011 }
2012
2013 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2014 including converting functions and arrays to pointers if CONVERT_P.
2015 If READ_P, also mark the expression as having been read. */
2016
2017 struct c_expr
2018 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2019 bool convert_p, bool read_p)
2020 {
2021 if (read_p)
2022 mark_exp_read (exp.value);
2023 if (convert_p)
2024 exp = default_function_array_conversion (loc, exp);
2025 if (really_atomic_lvalue (exp.value))
2026 {
2027 vec<tree, va_gc> *params;
2028 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2029 tree expr_type = TREE_TYPE (exp.value);
2030 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2031 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2032
2033 gcc_assert (TYPE_ATOMIC (expr_type));
2034
2035 /* Expansion of a generic atomic load may require an addition
2036 element, so allocate enough to prevent a resize. */
2037 vec_alloc (params, 4);
2038
2039 /* Remove the qualifiers for the rest of the expressions and
2040 create the VAL temp variable to hold the RHS. */
2041 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2042 tmp = create_tmp_var (nonatomic_type);
2043 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2044 TREE_ADDRESSABLE (tmp) = 1;
2045 TREE_NO_WARNING (tmp) = 1;
2046
2047 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2048 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2049 params->quick_push (expr_addr);
2050 params->quick_push (tmp_addr);
2051 params->quick_push (seq_cst);
2052 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2053
2054 /* EXPR is always read. */
2055 mark_exp_read (exp.value);
2056
2057 /* Return tmp which contains the value loaded. */
2058 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2059 }
2060 return exp;
2061 }
2062
2063 /* EXP is an expression of integer type. Apply the integer promotions
2064 to it and return the promoted value. */
2065
2066 tree
2067 perform_integral_promotions (tree exp)
2068 {
2069 tree type = TREE_TYPE (exp);
2070 enum tree_code code = TREE_CODE (type);
2071
2072 gcc_assert (INTEGRAL_TYPE_P (type));
2073
2074 /* Normally convert enums to int,
2075 but convert wide enums to something wider. */
2076 if (code == ENUMERAL_TYPE)
2077 {
2078 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2079 TYPE_PRECISION (integer_type_node)),
2080 ((TYPE_PRECISION (type)
2081 >= TYPE_PRECISION (integer_type_node))
2082 && TYPE_UNSIGNED (type)));
2083
2084 return convert (type, exp);
2085 }
2086
2087 /* ??? This should no longer be needed now bit-fields have their
2088 proper types. */
2089 if (TREE_CODE (exp) == COMPONENT_REF
2090 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2091 /* If it's thinner than an int, promote it like a
2092 c_promoting_integer_type_p, otherwise leave it alone. */
2093 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2094 TYPE_PRECISION (integer_type_node)))
2095 return convert (integer_type_node, exp);
2096
2097 if (c_promoting_integer_type_p (type))
2098 {
2099 /* Preserve unsignedness if not really getting any wider. */
2100 if (TYPE_UNSIGNED (type)
2101 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2102 return convert (unsigned_type_node, exp);
2103
2104 return convert (integer_type_node, exp);
2105 }
2106
2107 return exp;
2108 }
2109
2110
2111 /* Perform default promotions for C data used in expressions.
2112 Enumeral types or short or char are converted to int.
2113 In addition, manifest constants symbols are replaced by their values. */
2114
2115 tree
2116 default_conversion (tree exp)
2117 {
2118 tree orig_exp;
2119 tree type = TREE_TYPE (exp);
2120 enum tree_code code = TREE_CODE (type);
2121 tree promoted_type;
2122
2123 mark_exp_read (exp);
2124
2125 /* Functions and arrays have been converted during parsing. */
2126 gcc_assert (code != FUNCTION_TYPE);
2127 if (code == ARRAY_TYPE)
2128 return exp;
2129
2130 /* Constants can be used directly unless they're not loadable. */
2131 if (TREE_CODE (exp) == CONST_DECL)
2132 exp = DECL_INITIAL (exp);
2133
2134 /* Strip no-op conversions. */
2135 orig_exp = exp;
2136 STRIP_TYPE_NOPS (exp);
2137
2138 if (TREE_NO_WARNING (orig_exp))
2139 TREE_NO_WARNING (exp) = 1;
2140
2141 if (code == VOID_TYPE)
2142 {
2143 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2144 "void value not ignored as it ought to be");
2145 return error_mark_node;
2146 }
2147
2148 exp = require_complete_type (exp);
2149 if (exp == error_mark_node)
2150 return error_mark_node;
2151
2152 promoted_type = targetm.promoted_type (type);
2153 if (promoted_type)
2154 return convert (promoted_type, exp);
2155
2156 if (INTEGRAL_TYPE_P (type))
2157 return perform_integral_promotions (exp);
2158
2159 return exp;
2160 }
2161 \f
2162 /* Look up COMPONENT in a structure or union TYPE.
2163
2164 If the component name is not found, returns NULL_TREE. Otherwise,
2165 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2166 stepping down the chain to the component, which is in the last
2167 TREE_VALUE of the list. Normally the list is of length one, but if
2168 the component is embedded within (nested) anonymous structures or
2169 unions, the list steps down the chain to the component. */
2170
2171 static tree
2172 lookup_field (tree type, tree component)
2173 {
2174 tree field;
2175
2176 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2177 to the field elements. Use a binary search on this array to quickly
2178 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2179 will always be set for structures which have many elements. */
2180
2181 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2182 {
2183 int bot, top, half;
2184 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2185
2186 field = TYPE_FIELDS (type);
2187 bot = 0;
2188 top = TYPE_LANG_SPECIFIC (type)->s->len;
2189 while (top - bot > 1)
2190 {
2191 half = (top - bot + 1) >> 1;
2192 field = field_array[bot+half];
2193
2194 if (DECL_NAME (field) == NULL_TREE)
2195 {
2196 /* Step through all anon unions in linear fashion. */
2197 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2198 {
2199 field = field_array[bot++];
2200 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2201 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2202 {
2203 tree anon = lookup_field (TREE_TYPE (field), component);
2204
2205 if (anon)
2206 return tree_cons (NULL_TREE, field, anon);
2207
2208 /* The Plan 9 compiler permits referring
2209 directly to an anonymous struct/union field
2210 using a typedef name. */
2211 if (flag_plan9_extensions
2212 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2213 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2214 == TYPE_DECL)
2215 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2216 == component))
2217 break;
2218 }
2219 }
2220
2221 /* Entire record is only anon unions. */
2222 if (bot > top)
2223 return NULL_TREE;
2224
2225 /* Restart the binary search, with new lower bound. */
2226 continue;
2227 }
2228
2229 if (DECL_NAME (field) == component)
2230 break;
2231 if (DECL_NAME (field) < component)
2232 bot += half;
2233 else
2234 top = bot + half;
2235 }
2236
2237 if (DECL_NAME (field_array[bot]) == component)
2238 field = field_array[bot];
2239 else if (DECL_NAME (field) != component)
2240 return NULL_TREE;
2241 }
2242 else
2243 {
2244 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2245 {
2246 if (DECL_NAME (field) == NULL_TREE
2247 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2248 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2249 {
2250 tree anon = lookup_field (TREE_TYPE (field), component);
2251
2252 if (anon)
2253 return tree_cons (NULL_TREE, field, anon);
2254
2255 /* The Plan 9 compiler permits referring directly to an
2256 anonymous struct/union field using a typedef
2257 name. */
2258 if (flag_plan9_extensions
2259 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2260 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2261 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2262 == component))
2263 break;
2264 }
2265
2266 if (DECL_NAME (field) == component)
2267 break;
2268 }
2269
2270 if (field == NULL_TREE)
2271 return NULL_TREE;
2272 }
2273
2274 return tree_cons (NULL_TREE, field, NULL_TREE);
2275 }
2276
2277 /* Make an expression to refer to the COMPONENT field of structure or
2278 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2279 location of the COMPONENT_REF. */
2280
2281 tree
2282 build_component_ref (location_t loc, tree datum, tree component)
2283 {
2284 tree type = TREE_TYPE (datum);
2285 enum tree_code code = TREE_CODE (type);
2286 tree field = NULL;
2287 tree ref;
2288 bool datum_lvalue = lvalue_p (datum);
2289
2290 if (!objc_is_public (datum, component))
2291 return error_mark_node;
2292
2293 /* Detect Objective-C property syntax object.property. */
2294 if (c_dialect_objc ()
2295 && (ref = objc_maybe_build_component_ref (datum, component)))
2296 return ref;
2297
2298 /* See if there is a field or component with name COMPONENT. */
2299
2300 if (code == RECORD_TYPE || code == UNION_TYPE)
2301 {
2302 if (!COMPLETE_TYPE_P (type))
2303 {
2304 c_incomplete_type_error (NULL_TREE, type);
2305 return error_mark_node;
2306 }
2307
2308 field = lookup_field (type, component);
2309
2310 if (!field)
2311 {
2312 error_at (loc, "%qT has no member named %qE", type, component);
2313 return error_mark_node;
2314 }
2315
2316 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2317 This might be better solved in future the way the C++ front
2318 end does it - by giving the anonymous entities each a
2319 separate name and type, and then have build_component_ref
2320 recursively call itself. We can't do that here. */
2321 do
2322 {
2323 tree subdatum = TREE_VALUE (field);
2324 int quals;
2325 tree subtype;
2326 bool use_datum_quals;
2327
2328 if (TREE_TYPE (subdatum) == error_mark_node)
2329 return error_mark_node;
2330
2331 /* If this is an rvalue, it does not have qualifiers in C
2332 standard terms and we must avoid propagating such
2333 qualifiers down to a non-lvalue array that is then
2334 converted to a pointer. */
2335 use_datum_quals = (datum_lvalue
2336 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2337
2338 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2339 if (use_datum_quals)
2340 quals |= TYPE_QUALS (TREE_TYPE (datum));
2341 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2342
2343 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2344 NULL_TREE);
2345 SET_EXPR_LOCATION (ref, loc);
2346 if (TREE_READONLY (subdatum)
2347 || (use_datum_quals && TREE_READONLY (datum)))
2348 TREE_READONLY (ref) = 1;
2349 if (TREE_THIS_VOLATILE (subdatum)
2350 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2351 TREE_THIS_VOLATILE (ref) = 1;
2352
2353 if (TREE_DEPRECATED (subdatum))
2354 warn_deprecated_use (subdatum, NULL_TREE);
2355
2356 datum = ref;
2357
2358 field = TREE_CHAIN (field);
2359 }
2360 while (field);
2361
2362 return ref;
2363 }
2364 else if (code != ERROR_MARK)
2365 error_at (loc,
2366 "request for member %qE in something not a structure or union",
2367 component);
2368
2369 return error_mark_node;
2370 }
2371 \f
2372 /* Given an expression PTR for a pointer, return an expression
2373 for the value pointed to.
2374 ERRORSTRING is the name of the operator to appear in error messages.
2375
2376 LOC is the location to use for the generated tree. */
2377
2378 tree
2379 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2380 {
2381 tree pointer = default_conversion (ptr);
2382 tree type = TREE_TYPE (pointer);
2383 tree ref;
2384
2385 if (TREE_CODE (type) == POINTER_TYPE)
2386 {
2387 if (CONVERT_EXPR_P (pointer)
2388 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2389 {
2390 /* If a warning is issued, mark it to avoid duplicates from
2391 the backend. This only needs to be done at
2392 warn_strict_aliasing > 2. */
2393 if (warn_strict_aliasing > 2)
2394 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2395 type, TREE_OPERAND (pointer, 0)))
2396 TREE_NO_WARNING (pointer) = 1;
2397 }
2398
2399 if (TREE_CODE (pointer) == ADDR_EXPR
2400 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2401 == TREE_TYPE (type)))
2402 {
2403 ref = TREE_OPERAND (pointer, 0);
2404 protected_set_expr_location (ref, loc);
2405 return ref;
2406 }
2407 else
2408 {
2409 tree t = TREE_TYPE (type);
2410
2411 ref = build1 (INDIRECT_REF, t, pointer);
2412
2413 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2414 {
2415 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2416 {
2417 error_at (loc, "dereferencing pointer to incomplete type "
2418 "%qT", t);
2419 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2420 }
2421 return error_mark_node;
2422 }
2423 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2424 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2425
2426 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2427 so that we get the proper error message if the result is used
2428 to assign to. Also, &* is supposed to be a no-op.
2429 And ANSI C seems to specify that the type of the result
2430 should be the const type. */
2431 /* A de-reference of a pointer to const is not a const. It is valid
2432 to change it via some other pointer. */
2433 TREE_READONLY (ref) = TYPE_READONLY (t);
2434 TREE_SIDE_EFFECTS (ref)
2435 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2436 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2437 protected_set_expr_location (ref, loc);
2438 return ref;
2439 }
2440 }
2441 else if (TREE_CODE (pointer) != ERROR_MARK)
2442 invalid_indirection_error (loc, type, errstring);
2443
2444 return error_mark_node;
2445 }
2446
2447 /* This handles expressions of the form "a[i]", which denotes
2448 an array reference.
2449
2450 This is logically equivalent in C to *(a+i), but we may do it differently.
2451 If A is a variable or a member, we generate a primitive ARRAY_REF.
2452 This avoids forcing the array out of registers, and can work on
2453 arrays that are not lvalues (for example, members of structures returned
2454 by functions).
2455
2456 For vector types, allow vector[i] but not i[vector], and create
2457 *(((type*)&vectortype) + i) for the expression.
2458
2459 LOC is the location to use for the returned expression. */
2460
2461 tree
2462 build_array_ref (location_t loc, tree array, tree index)
2463 {
2464 tree ret;
2465 bool swapped = false;
2466 if (TREE_TYPE (array) == error_mark_node
2467 || TREE_TYPE (index) == error_mark_node)
2468 return error_mark_node;
2469
2470 if (flag_cilkplus && contains_array_notation_expr (index))
2471 {
2472 size_t rank = 0;
2473 if (!find_rank (loc, index, index, true, &rank))
2474 return error_mark_node;
2475 if (rank > 1)
2476 {
2477 error_at (loc, "rank of the array's index is greater than 1");
2478 return error_mark_node;
2479 }
2480 }
2481 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2482 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2483 /* Allow vector[index] but not index[vector]. */
2484 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2485 {
2486 tree temp;
2487 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2488 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2489 {
2490 error_at (loc,
2491 "subscripted value is neither array nor pointer nor vector");
2492
2493 return error_mark_node;
2494 }
2495 temp = array;
2496 array = index;
2497 index = temp;
2498 swapped = true;
2499 }
2500
2501 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2502 {
2503 error_at (loc, "array subscript is not an integer");
2504 return error_mark_node;
2505 }
2506
2507 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2508 {
2509 error_at (loc, "subscripted value is pointer to function");
2510 return error_mark_node;
2511 }
2512
2513 /* ??? Existing practice has been to warn only when the char
2514 index is syntactically the index, not for char[array]. */
2515 if (!swapped)
2516 warn_array_subscript_with_type_char (loc, index);
2517
2518 /* Apply default promotions *after* noticing character types. */
2519 index = default_conversion (index);
2520 if (index == error_mark_node)
2521 return error_mark_node;
2522
2523 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2524
2525 bool non_lvalue
2526 = convert_vector_to_pointer_for_subscript (loc, &array, index);
2527
2528 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2529 {
2530 tree rval, type;
2531
2532 /* An array that is indexed by a non-constant
2533 cannot be stored in a register; we must be able to do
2534 address arithmetic on its address.
2535 Likewise an array of elements of variable size. */
2536 if (TREE_CODE (index) != INTEGER_CST
2537 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2538 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2539 {
2540 if (!c_mark_addressable (array))
2541 return error_mark_node;
2542 }
2543 /* An array that is indexed by a constant value which is not within
2544 the array bounds cannot be stored in a register either; because we
2545 would get a crash in store_bit_field/extract_bit_field when trying
2546 to access a non-existent part of the register. */
2547 if (TREE_CODE (index) == INTEGER_CST
2548 && TYPE_DOMAIN (TREE_TYPE (array))
2549 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2550 {
2551 if (!c_mark_addressable (array))
2552 return error_mark_node;
2553 }
2554
2555 if (pedantic || warn_c90_c99_compat)
2556 {
2557 tree foo = array;
2558 while (TREE_CODE (foo) == COMPONENT_REF)
2559 foo = TREE_OPERAND (foo, 0);
2560 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2561 pedwarn (loc, OPT_Wpedantic,
2562 "ISO C forbids subscripting %<register%> array");
2563 else if (!lvalue_p (foo))
2564 pedwarn_c90 (loc, OPT_Wpedantic,
2565 "ISO C90 forbids subscripting non-lvalue "
2566 "array");
2567 }
2568
2569 type = TREE_TYPE (TREE_TYPE (array));
2570 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2571 /* Array ref is const/volatile if the array elements are
2572 or if the array is. */
2573 TREE_READONLY (rval)
2574 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2575 | TREE_READONLY (array));
2576 TREE_SIDE_EFFECTS (rval)
2577 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2578 | TREE_SIDE_EFFECTS (array));
2579 TREE_THIS_VOLATILE (rval)
2580 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2581 /* This was added by rms on 16 Nov 91.
2582 It fixes vol struct foo *a; a->elts[1]
2583 in an inline function.
2584 Hope it doesn't break something else. */
2585 | TREE_THIS_VOLATILE (array));
2586 ret = require_complete_type (rval);
2587 protected_set_expr_location (ret, loc);
2588 if (non_lvalue)
2589 ret = non_lvalue_loc (loc, ret);
2590 return ret;
2591 }
2592 else
2593 {
2594 tree ar = default_conversion (array);
2595
2596 if (ar == error_mark_node)
2597 return ar;
2598
2599 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2600 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2601
2602 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2603 index, 0),
2604 RO_ARRAY_INDEXING);
2605 if (non_lvalue)
2606 ret = non_lvalue_loc (loc, ret);
2607 return ret;
2608 }
2609 }
2610 \f
2611 /* Build an external reference to identifier ID. FUN indicates
2612 whether this will be used for a function call. LOC is the source
2613 location of the identifier. This sets *TYPE to the type of the
2614 identifier, which is not the same as the type of the returned value
2615 for CONST_DECLs defined as enum constants. If the type of the
2616 identifier is not available, *TYPE is set to NULL. */
2617 tree
2618 build_external_ref (location_t loc, tree id, int fun, tree *type)
2619 {
2620 tree ref;
2621 tree decl = lookup_name (id);
2622
2623 /* In Objective-C, an instance variable (ivar) may be preferred to
2624 whatever lookup_name() found. */
2625 decl = objc_lookup_ivar (decl, id);
2626
2627 *type = NULL;
2628 if (decl && decl != error_mark_node)
2629 {
2630 ref = decl;
2631 *type = TREE_TYPE (ref);
2632 }
2633 else if (fun)
2634 /* Implicit function declaration. */
2635 ref = implicitly_declare (loc, id);
2636 else if (decl == error_mark_node)
2637 /* Don't complain about something that's already been
2638 complained about. */
2639 return error_mark_node;
2640 else
2641 {
2642 undeclared_variable (loc, id);
2643 return error_mark_node;
2644 }
2645
2646 if (TREE_TYPE (ref) == error_mark_node)
2647 return error_mark_node;
2648
2649 if (TREE_DEPRECATED (ref))
2650 warn_deprecated_use (ref, NULL_TREE);
2651
2652 /* Recursive call does not count as usage. */
2653 if (ref != current_function_decl)
2654 {
2655 TREE_USED (ref) = 1;
2656 }
2657
2658 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2659 {
2660 if (!in_sizeof && !in_typeof)
2661 C_DECL_USED (ref) = 1;
2662 else if (DECL_INITIAL (ref) == 0
2663 && DECL_EXTERNAL (ref)
2664 && !TREE_PUBLIC (ref))
2665 record_maybe_used_decl (ref);
2666 }
2667
2668 if (TREE_CODE (ref) == CONST_DECL)
2669 {
2670 used_types_insert (TREE_TYPE (ref));
2671
2672 if (warn_cxx_compat
2673 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2674 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2675 {
2676 warning_at (loc, OPT_Wc___compat,
2677 ("enum constant defined in struct or union "
2678 "is not visible in C++"));
2679 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2680 }
2681
2682 ref = DECL_INITIAL (ref);
2683 TREE_CONSTANT (ref) = 1;
2684 }
2685 else if (current_function_decl != 0
2686 && !DECL_FILE_SCOPE_P (current_function_decl)
2687 && (TREE_CODE (ref) == VAR_DECL
2688 || TREE_CODE (ref) == PARM_DECL
2689 || TREE_CODE (ref) == FUNCTION_DECL))
2690 {
2691 tree context = decl_function_context (ref);
2692
2693 if (context != 0 && context != current_function_decl)
2694 DECL_NONLOCAL (ref) = 1;
2695 }
2696 /* C99 6.7.4p3: An inline definition of a function with external
2697 linkage ... shall not contain a reference to an identifier with
2698 internal linkage. */
2699 else if (current_function_decl != 0
2700 && DECL_DECLARED_INLINE_P (current_function_decl)
2701 && DECL_EXTERNAL (current_function_decl)
2702 && VAR_OR_FUNCTION_DECL_P (ref)
2703 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2704 && ! TREE_PUBLIC (ref)
2705 && DECL_CONTEXT (ref) != current_function_decl)
2706 record_inline_static (loc, current_function_decl, ref,
2707 csi_internal);
2708
2709 return ref;
2710 }
2711
2712 /* Record details of decls possibly used inside sizeof or typeof. */
2713 struct maybe_used_decl
2714 {
2715 /* The decl. */
2716 tree decl;
2717 /* The level seen at (in_sizeof + in_typeof). */
2718 int level;
2719 /* The next one at this level or above, or NULL. */
2720 struct maybe_used_decl *next;
2721 };
2722
2723 static struct maybe_used_decl *maybe_used_decls;
2724
2725 /* Record that DECL, an undefined static function reference seen
2726 inside sizeof or typeof, might be used if the operand of sizeof is
2727 a VLA type or the operand of typeof is a variably modified
2728 type. */
2729
2730 static void
2731 record_maybe_used_decl (tree decl)
2732 {
2733 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2734 t->decl = decl;
2735 t->level = in_sizeof + in_typeof;
2736 t->next = maybe_used_decls;
2737 maybe_used_decls = t;
2738 }
2739
2740 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2741 USED is false, just discard them. If it is true, mark them used
2742 (if no longer inside sizeof or typeof) or move them to the next
2743 level up (if still inside sizeof or typeof). */
2744
2745 void
2746 pop_maybe_used (bool used)
2747 {
2748 struct maybe_used_decl *p = maybe_used_decls;
2749 int cur_level = in_sizeof + in_typeof;
2750 while (p && p->level > cur_level)
2751 {
2752 if (used)
2753 {
2754 if (cur_level == 0)
2755 C_DECL_USED (p->decl) = 1;
2756 else
2757 p->level = cur_level;
2758 }
2759 p = p->next;
2760 }
2761 if (!used || cur_level == 0)
2762 maybe_used_decls = p;
2763 }
2764
2765 /* Return the result of sizeof applied to EXPR. */
2766
2767 struct c_expr
2768 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2769 {
2770 struct c_expr ret;
2771 if (expr.value == error_mark_node)
2772 {
2773 ret.value = error_mark_node;
2774 ret.original_code = ERROR_MARK;
2775 ret.original_type = NULL;
2776 pop_maybe_used (false);
2777 }
2778 else
2779 {
2780 bool expr_const_operands = true;
2781
2782 if (TREE_CODE (expr.value) == PARM_DECL
2783 && C_ARRAY_PARAMETER (expr.value))
2784 {
2785 if (warning_at (loc, OPT_Wsizeof_array_argument,
2786 "%<sizeof%> on array function parameter %qE will "
2787 "return size of %qT", expr.value,
2788 expr.original_type))
2789 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2790 }
2791 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2792 &expr_const_operands);
2793 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2794 c_last_sizeof_arg = expr.value;
2795 ret.original_code = SIZEOF_EXPR;
2796 ret.original_type = NULL;
2797 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2798 {
2799 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2800 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2801 folded_expr, ret.value);
2802 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2803 SET_EXPR_LOCATION (ret.value, loc);
2804 }
2805 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2806 }
2807 return ret;
2808 }
2809
2810 /* Return the result of sizeof applied to T, a structure for the type
2811 name passed to sizeof (rather than the type itself). LOC is the
2812 location of the original expression. */
2813
2814 struct c_expr
2815 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2816 {
2817 tree type;
2818 struct c_expr ret;
2819 tree type_expr = NULL_TREE;
2820 bool type_expr_const = true;
2821 type = groktypename (t, &type_expr, &type_expr_const);
2822 ret.value = c_sizeof (loc, type);
2823 c_last_sizeof_arg = type;
2824 ret.original_code = SIZEOF_EXPR;
2825 ret.original_type = NULL;
2826 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2827 && c_vla_type_p (type))
2828 {
2829 /* If the type is a [*] array, it is a VLA but is represented as
2830 having a size of zero. In such a case we must ensure that
2831 the result of sizeof does not get folded to a constant by
2832 c_fully_fold, because if the size is evaluated the result is
2833 not constant and so constraints on zero or negative size
2834 arrays must not be applied when this sizeof call is inside
2835 another array declarator. */
2836 if (!type_expr)
2837 type_expr = integer_zero_node;
2838 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2839 type_expr, ret.value);
2840 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2841 }
2842 pop_maybe_used (type != error_mark_node
2843 ? C_TYPE_VARIABLE_SIZE (type) : false);
2844 return ret;
2845 }
2846
2847 /* Build a function call to function FUNCTION with parameters PARAMS.
2848 The function call is at LOC.
2849 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2850 TREE_VALUE of each node is a parameter-expression.
2851 FUNCTION's data type may be a function type or a pointer-to-function. */
2852
2853 tree
2854 build_function_call (location_t loc, tree function, tree params)
2855 {
2856 vec<tree, va_gc> *v;
2857 tree ret;
2858
2859 vec_alloc (v, list_length (params));
2860 for (; params; params = TREE_CHAIN (params))
2861 v->quick_push (TREE_VALUE (params));
2862 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2863 vec_free (v);
2864 return ret;
2865 }
2866
2867 /* Give a note about the location of the declaration of DECL. */
2868
2869 static void inform_declaration (tree decl)
2870 {
2871 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2872 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2873 }
2874
2875 /* Build a function call to function FUNCTION with parameters PARAMS.
2876 ORIGTYPES, if not NULL, is a vector of types; each element is
2877 either NULL or the original type of the corresponding element in
2878 PARAMS. The original type may differ from TREE_TYPE of the
2879 parameter for enums. FUNCTION's data type may be a function type
2880 or pointer-to-function. This function changes the elements of
2881 PARAMS. */
2882
2883 tree
2884 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2885 tree function, vec<tree, va_gc> *params,
2886 vec<tree, va_gc> *origtypes)
2887 {
2888 tree fntype, fundecl = 0;
2889 tree name = NULL_TREE, result;
2890 tree tem;
2891 int nargs;
2892 tree *argarray;
2893
2894
2895 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2896 STRIP_TYPE_NOPS (function);
2897
2898 /* Convert anything with function type to a pointer-to-function. */
2899 if (TREE_CODE (function) == FUNCTION_DECL)
2900 {
2901 name = DECL_NAME (function);
2902
2903 if (flag_tm)
2904 tm_malloc_replacement (function);
2905 fundecl = function;
2906 /* Atomic functions have type checking/casting already done. They are
2907 often rewritten and don't match the original parameter list. */
2908 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2909 origtypes = NULL;
2910
2911 if (flag_cilkplus
2912 && is_cilkplus_reduce_builtin (function))
2913 origtypes = NULL;
2914 }
2915 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2916 function = function_to_pointer_conversion (loc, function);
2917
2918 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2919 expressions, like those used for ObjC messenger dispatches. */
2920 if (params && !params->is_empty ())
2921 function = objc_rewrite_function_call (function, (*params)[0]);
2922
2923 function = c_fully_fold (function, false, NULL);
2924
2925 fntype = TREE_TYPE (function);
2926
2927 if (TREE_CODE (fntype) == ERROR_MARK)
2928 return error_mark_node;
2929
2930 if (!(TREE_CODE (fntype) == POINTER_TYPE
2931 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2932 {
2933 if (!flag_diagnostics_show_caret)
2934 error_at (loc,
2935 "called object %qE is not a function or function pointer",
2936 function);
2937 else if (DECL_P (function))
2938 {
2939 error_at (loc,
2940 "called object %qD is not a function or function pointer",
2941 function);
2942 inform_declaration (function);
2943 }
2944 else
2945 error_at (loc,
2946 "called object is not a function or function pointer");
2947 return error_mark_node;
2948 }
2949
2950 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2951 current_function_returns_abnormally = 1;
2952
2953 /* fntype now gets the type of function pointed to. */
2954 fntype = TREE_TYPE (fntype);
2955
2956 /* Convert the parameters to the types declared in the
2957 function prototype, or apply default promotions. */
2958
2959 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2960 origtypes, function, fundecl);
2961 if (nargs < 0)
2962 return error_mark_node;
2963
2964 /* Check that the function is called through a compatible prototype.
2965 If it is not, warn. */
2966 if (CONVERT_EXPR_P (function)
2967 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2968 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2969 && !comptypes (fntype, TREE_TYPE (tem)))
2970 {
2971 tree return_type = TREE_TYPE (fntype);
2972
2973 /* This situation leads to run-time undefined behavior. We can't,
2974 therefore, simply error unless we can prove that all possible
2975 executions of the program must execute the code. */
2976 warning_at (loc, 0, "function called through a non-compatible type");
2977
2978 if (VOID_TYPE_P (return_type)
2979 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2980 pedwarn (loc, 0,
2981 "function with qualified void return type called");
2982 }
2983
2984 argarray = vec_safe_address (params);
2985
2986 /* Check that arguments to builtin functions match the expectations. */
2987 if (fundecl
2988 && DECL_BUILT_IN (fundecl)
2989 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2990 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2991 return error_mark_node;
2992
2993 /* Check that the arguments to the function are valid. */
2994 check_function_arguments (fntype, nargs, argarray);
2995
2996 if (name != NULL_TREE
2997 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2998 {
2999 if (require_constant_value)
3000 result =
3001 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3002 function, nargs, argarray);
3003 else
3004 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3005 function, nargs, argarray);
3006 if (TREE_CODE (result) == NOP_EXPR
3007 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3008 STRIP_TYPE_NOPS (result);
3009 }
3010 else
3011 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3012 function, nargs, argarray);
3013
3014 if (VOID_TYPE_P (TREE_TYPE (result)))
3015 {
3016 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3017 pedwarn (loc, 0,
3018 "function with qualified void return type called");
3019 return result;
3020 }
3021 return require_complete_type (result);
3022 }
3023
3024 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3025
3026 tree
3027 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3028 tree function, vec<tree, va_gc> *params,
3029 vec<tree, va_gc> *origtypes)
3030 {
3031 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3032 STRIP_TYPE_NOPS (function);
3033
3034 /* Convert anything with function type to a pointer-to-function. */
3035 if (TREE_CODE (function) == FUNCTION_DECL)
3036 {
3037 /* Implement type-directed function overloading for builtins.
3038 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3039 handle all the type checking. The result is a complete expression
3040 that implements this function call. */
3041 tree tem = resolve_overloaded_builtin (loc, function, params);
3042 if (tem)
3043 return tem;
3044 }
3045 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3046 }
3047 \f
3048 /* Convert the argument expressions in the vector VALUES
3049 to the types in the list TYPELIST.
3050
3051 If TYPELIST is exhausted, or when an element has NULL as its type,
3052 perform the default conversions.
3053
3054 ORIGTYPES is the original types of the expressions in VALUES. This
3055 holds the type of enum values which have been converted to integral
3056 types. It may be NULL.
3057
3058 FUNCTION is a tree for the called function. It is used only for
3059 error messages, where it is formatted with %qE.
3060
3061 This is also where warnings about wrong number of args are generated.
3062
3063 ARG_LOC are locations of function arguments (if any).
3064
3065 Returns the actual number of arguments processed (which may be less
3066 than the length of VALUES in some error situations), or -1 on
3067 failure. */
3068
3069 static int
3070 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3071 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3072 tree function, tree fundecl)
3073 {
3074 tree typetail, val;
3075 unsigned int parmnum;
3076 bool error_args = false;
3077 const bool type_generic = fundecl
3078 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3079 bool type_generic_remove_excess_precision = false;
3080 tree selector;
3081
3082 /* Change pointer to function to the function itself for
3083 diagnostics. */
3084 if (TREE_CODE (function) == ADDR_EXPR
3085 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3086 function = TREE_OPERAND (function, 0);
3087
3088 /* Handle an ObjC selector specially for diagnostics. */
3089 selector = objc_message_selector ();
3090
3091 /* For type-generic built-in functions, determine whether excess
3092 precision should be removed (classification) or not
3093 (comparison). */
3094 if (type_generic
3095 && DECL_BUILT_IN (fundecl)
3096 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3097 {
3098 switch (DECL_FUNCTION_CODE (fundecl))
3099 {
3100 case BUILT_IN_ISFINITE:
3101 case BUILT_IN_ISINF:
3102 case BUILT_IN_ISINF_SIGN:
3103 case BUILT_IN_ISNAN:
3104 case BUILT_IN_ISNORMAL:
3105 case BUILT_IN_FPCLASSIFY:
3106 type_generic_remove_excess_precision = true;
3107 break;
3108
3109 default:
3110 type_generic_remove_excess_precision = false;
3111 break;
3112 }
3113 }
3114 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3115 return vec_safe_length (values);
3116
3117 /* Scan the given expressions and types, producing individual
3118 converted arguments. */
3119
3120 for (typetail = typelist, parmnum = 0;
3121 values && values->iterate (parmnum, &val);
3122 ++parmnum)
3123 {
3124 tree type = typetail ? TREE_VALUE (typetail) : 0;
3125 tree valtype = TREE_TYPE (val);
3126 tree rname = function;
3127 int argnum = parmnum + 1;
3128 const char *invalid_func_diag;
3129 bool excess_precision = false;
3130 bool npc;
3131 tree parmval;
3132 /* Some __atomic_* builtins have additional hidden argument at
3133 position 0. */
3134 location_t ploc
3135 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3136 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3137 : input_location;
3138
3139 if (type == void_type_node)
3140 {
3141 if (selector)
3142 error_at (loc, "too many arguments to method %qE", selector);
3143 else
3144 error_at (loc, "too many arguments to function %qE", function);
3145 inform_declaration (fundecl);
3146 return error_args ? -1 : (int) parmnum;
3147 }
3148
3149 if (selector && argnum > 2)
3150 {
3151 rname = selector;
3152 argnum -= 2;
3153 }
3154
3155 npc = null_pointer_constant_p (val);
3156
3157 /* If there is excess precision and a prototype, convert once to
3158 the required type rather than converting via the semantic
3159 type. Likewise without a prototype a float value represented
3160 as long double should be converted once to double. But for
3161 type-generic classification functions excess precision must
3162 be removed here. */
3163 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3164 && (type || !type_generic || !type_generic_remove_excess_precision))
3165 {
3166 val = TREE_OPERAND (val, 0);
3167 excess_precision = true;
3168 }
3169 val = c_fully_fold (val, false, NULL);
3170 STRIP_TYPE_NOPS (val);
3171
3172 val = require_complete_type (val);
3173
3174 if (type != 0)
3175 {
3176 /* Formal parm type is specified by a function prototype. */
3177
3178 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3179 {
3180 error_at (ploc, "type of formal parameter %d is incomplete",
3181 parmnum + 1);
3182 parmval = val;
3183 }
3184 else
3185 {
3186 tree origtype;
3187
3188 /* Optionally warn about conversions that
3189 differ from the default conversions. */
3190 if (warn_traditional_conversion || warn_traditional)
3191 {
3192 unsigned int formal_prec = TYPE_PRECISION (type);
3193
3194 if (INTEGRAL_TYPE_P (type)
3195 && TREE_CODE (valtype) == REAL_TYPE)
3196 warning_at (ploc, OPT_Wtraditional_conversion,
3197 "passing argument %d of %qE as integer rather "
3198 "than floating due to prototype",
3199 argnum, rname);
3200 if (INTEGRAL_TYPE_P (type)
3201 && TREE_CODE (valtype) == COMPLEX_TYPE)
3202 warning_at (ploc, OPT_Wtraditional_conversion,
3203 "passing argument %d of %qE as integer rather "
3204 "than complex due to prototype",
3205 argnum, rname);
3206 else if (TREE_CODE (type) == COMPLEX_TYPE
3207 && TREE_CODE (valtype) == REAL_TYPE)
3208 warning_at (ploc, OPT_Wtraditional_conversion,
3209 "passing argument %d of %qE as complex rather "
3210 "than floating due to prototype",
3211 argnum, rname);
3212 else if (TREE_CODE (type) == REAL_TYPE
3213 && INTEGRAL_TYPE_P (valtype))
3214 warning_at (ploc, OPT_Wtraditional_conversion,
3215 "passing argument %d of %qE as floating rather "
3216 "than integer due to prototype",
3217 argnum, rname);
3218 else if (TREE_CODE (type) == COMPLEX_TYPE
3219 && INTEGRAL_TYPE_P (valtype))
3220 warning_at (ploc, OPT_Wtraditional_conversion,
3221 "passing argument %d of %qE as complex rather "
3222 "than integer due to prototype",
3223 argnum, rname);
3224 else if (TREE_CODE (type) == REAL_TYPE
3225 && TREE_CODE (valtype) == COMPLEX_TYPE)
3226 warning_at (ploc, OPT_Wtraditional_conversion,
3227 "passing argument %d of %qE as floating rather "
3228 "than complex due to prototype",
3229 argnum, rname);
3230 /* ??? At some point, messages should be written about
3231 conversions between complex types, but that's too messy
3232 to do now. */
3233 else if (TREE_CODE (type) == REAL_TYPE
3234 && TREE_CODE (valtype) == REAL_TYPE)
3235 {
3236 /* Warn if any argument is passed as `float',
3237 since without a prototype it would be `double'. */
3238 if (formal_prec == TYPE_PRECISION (float_type_node)
3239 && type != dfloat32_type_node)
3240 warning_at (ploc, 0,
3241 "passing argument %d of %qE as %<float%> "
3242 "rather than %<double%> due to prototype",
3243 argnum, rname);
3244
3245 /* Warn if mismatch between argument and prototype
3246 for decimal float types. Warn of conversions with
3247 binary float types and of precision narrowing due to
3248 prototype. */
3249 else if (type != valtype
3250 && (type == dfloat32_type_node
3251 || type == dfloat64_type_node
3252 || type == dfloat128_type_node
3253 || valtype == dfloat32_type_node
3254 || valtype == dfloat64_type_node
3255 || valtype == dfloat128_type_node)
3256 && (formal_prec
3257 <= TYPE_PRECISION (valtype)
3258 || (type == dfloat128_type_node
3259 && (valtype
3260 != dfloat64_type_node
3261 && (valtype
3262 != dfloat32_type_node)))
3263 || (type == dfloat64_type_node
3264 && (valtype
3265 != dfloat32_type_node))))
3266 warning_at (ploc, 0,
3267 "passing argument %d of %qE as %qT "
3268 "rather than %qT due to prototype",
3269 argnum, rname, type, valtype);
3270
3271 }
3272 /* Detect integer changing in width or signedness.
3273 These warnings are only activated with
3274 -Wtraditional-conversion, not with -Wtraditional. */
3275 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3276 && INTEGRAL_TYPE_P (valtype))
3277 {
3278 tree would_have_been = default_conversion (val);
3279 tree type1 = TREE_TYPE (would_have_been);
3280
3281 if (TREE_CODE (type) == ENUMERAL_TYPE
3282 && (TYPE_MAIN_VARIANT (type)
3283 == TYPE_MAIN_VARIANT (valtype)))
3284 /* No warning if function asks for enum
3285 and the actual arg is that enum type. */
3286 ;
3287 else if (formal_prec != TYPE_PRECISION (type1))
3288 warning_at (ploc, OPT_Wtraditional_conversion,
3289 "passing argument %d of %qE "
3290 "with different width due to prototype",
3291 argnum, rname);
3292 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3293 ;
3294 /* Don't complain if the formal parameter type
3295 is an enum, because we can't tell now whether
3296 the value was an enum--even the same enum. */
3297 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3298 ;
3299 else if (TREE_CODE (val) == INTEGER_CST
3300 && int_fits_type_p (val, type))
3301 /* Change in signedness doesn't matter
3302 if a constant value is unaffected. */
3303 ;
3304 /* If the value is extended from a narrower
3305 unsigned type, it doesn't matter whether we
3306 pass it as signed or unsigned; the value
3307 certainly is the same either way. */
3308 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3309 && TYPE_UNSIGNED (valtype))
3310 ;
3311 else if (TYPE_UNSIGNED (type))
3312 warning_at (ploc, OPT_Wtraditional_conversion,
3313 "passing argument %d of %qE "
3314 "as unsigned due to prototype",
3315 argnum, rname);
3316 else
3317 warning_at (ploc, OPT_Wtraditional_conversion,
3318 "passing argument %d of %qE "
3319 "as signed due to prototype",
3320 argnum, rname);
3321 }
3322 }
3323
3324 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3325 sake of better warnings from convert_and_check. */
3326 if (excess_precision)
3327 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3328 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3329 parmval = convert_for_assignment (loc, ploc, type,
3330 val, origtype, ic_argpass,
3331 npc, fundecl, function,
3332 parmnum + 1);
3333
3334 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3335 && INTEGRAL_TYPE_P (type)
3336 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3337 parmval = default_conversion (parmval);
3338 }
3339 }
3340 else if (TREE_CODE (valtype) == REAL_TYPE
3341 && (TYPE_PRECISION (valtype)
3342 <= TYPE_PRECISION (double_type_node))
3343 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3344 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3345 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3346 {
3347 if (type_generic)
3348 parmval = val;
3349 else
3350 {
3351 /* Convert `float' to `double'. */
3352 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3353 warning_at (ploc, OPT_Wdouble_promotion,
3354 "implicit conversion from %qT to %qT when passing "
3355 "argument to function",
3356 valtype, double_type_node);
3357 parmval = convert (double_type_node, val);
3358 }
3359 }
3360 else if (excess_precision && !type_generic)
3361 /* A "double" argument with excess precision being passed
3362 without a prototype or in variable arguments. */
3363 parmval = convert (valtype, val);
3364 else if ((invalid_func_diag =
3365 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3366 {
3367 error (invalid_func_diag);
3368 return -1;
3369 }
3370 else
3371 /* Convert `short' and `char' to full-size `int'. */
3372 parmval = default_conversion (val);
3373
3374 (*values)[parmnum] = parmval;
3375 if (parmval == error_mark_node)
3376 error_args = true;
3377
3378 if (typetail)
3379 typetail = TREE_CHAIN (typetail);
3380 }
3381
3382 gcc_assert (parmnum == vec_safe_length (values));
3383
3384 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3385 {
3386 error_at (loc, "too few arguments to function %qE", function);
3387 inform_declaration (fundecl);
3388 return -1;
3389 }
3390
3391 return error_args ? -1 : (int) parmnum;
3392 }
3393 \f
3394 /* This is the entry point used by the parser to build unary operators
3395 in the input. CODE, a tree_code, specifies the unary operator, and
3396 ARG is the operand. For unary plus, the C parser currently uses
3397 CONVERT_EXPR for code.
3398
3399 LOC is the location to use for the tree generated.
3400 */
3401
3402 struct c_expr
3403 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3404 {
3405 struct c_expr result;
3406
3407 result.value = build_unary_op (loc, code, arg.value, 0);
3408 result.original_code = code;
3409 result.original_type = NULL;
3410
3411 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3412 overflow_warning (loc, result.value);
3413
3414 return result;
3415 }
3416
3417 /* This is the entry point used by the parser to build binary operators
3418 in the input. CODE, a tree_code, specifies the binary operator, and
3419 ARG1 and ARG2 are the operands. In addition to constructing the
3420 expression, we check for operands that were written with other binary
3421 operators in a way that is likely to confuse the user.
3422
3423 LOCATION is the location of the binary operator. */
3424
3425 struct c_expr
3426 parser_build_binary_op (location_t location, enum tree_code code,
3427 struct c_expr arg1, struct c_expr arg2)
3428 {
3429 struct c_expr result;
3430
3431 enum tree_code code1 = arg1.original_code;
3432 enum tree_code code2 = arg2.original_code;
3433 tree type1 = (arg1.original_type
3434 ? arg1.original_type
3435 : TREE_TYPE (arg1.value));
3436 tree type2 = (arg2.original_type
3437 ? arg2.original_type
3438 : TREE_TYPE (arg2.value));
3439
3440 result.value = build_binary_op (location, code,
3441 arg1.value, arg2.value, 1);
3442 result.original_code = code;
3443 result.original_type = NULL;
3444
3445 if (TREE_CODE (result.value) == ERROR_MARK)
3446 return result;
3447
3448 if (location != UNKNOWN_LOCATION)
3449 protected_set_expr_location (result.value, location);
3450
3451 /* Check for cases such as x+y<<z which users are likely
3452 to misinterpret. */
3453 if (warn_parentheses)
3454 warn_about_parentheses (location, code, code1, arg1.value, code2,
3455 arg2.value);
3456
3457 if (warn_logical_op)
3458 warn_logical_operator (location, code, TREE_TYPE (result.value),
3459 code1, arg1.value, code2, arg2.value);
3460
3461 if (warn_logical_not_paren
3462 && code1 == TRUTH_NOT_EXPR
3463 && code2 != TRUTH_NOT_EXPR)
3464 warn_logical_not_parentheses (location, code, arg2.value);
3465
3466 /* Warn about comparisons against string literals, with the exception
3467 of testing for equality or inequality of a string literal with NULL. */
3468 if (code == EQ_EXPR || code == NE_EXPR)
3469 {
3470 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3471 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3472 warning_at (location, OPT_Waddress,
3473 "comparison with string literal results in unspecified behavior");
3474 }
3475 else if (TREE_CODE_CLASS (code) == tcc_comparison
3476 && (code1 == STRING_CST || code2 == STRING_CST))
3477 warning_at (location, OPT_Waddress,
3478 "comparison with string literal results in unspecified behavior");
3479
3480 if (TREE_OVERFLOW_P (result.value)
3481 && !TREE_OVERFLOW_P (arg1.value)
3482 && !TREE_OVERFLOW_P (arg2.value))
3483 overflow_warning (location, result.value);
3484
3485 /* Warn about comparisons of different enum types. */
3486 if (warn_enum_compare
3487 && TREE_CODE_CLASS (code) == tcc_comparison
3488 && TREE_CODE (type1) == ENUMERAL_TYPE
3489 && TREE_CODE (type2) == ENUMERAL_TYPE
3490 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3491 warning_at (location, OPT_Wenum_compare,
3492 "comparison between %qT and %qT",
3493 type1, type2);
3494
3495 return result;
3496 }
3497 \f
3498 /* Return a tree for the difference of pointers OP0 and OP1.
3499 The resulting tree has type int. */
3500
3501 static tree
3502 pointer_diff (location_t loc, tree op0, tree op1)
3503 {
3504 tree restype = ptrdiff_type_node;
3505 tree result, inttype;
3506
3507 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3508 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3509 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3510 tree orig_op1 = op1;
3511
3512 /* If the operands point into different address spaces, we need to
3513 explicitly convert them to pointers into the common address space
3514 before we can subtract the numerical address values. */
3515 if (as0 != as1)
3516 {
3517 addr_space_t as_common;
3518 tree common_type;
3519
3520 /* Determine the common superset address space. This is guaranteed
3521 to exist because the caller verified that comp_target_types
3522 returned non-zero. */
3523 if (!addr_space_superset (as0, as1, &as_common))
3524 gcc_unreachable ();
3525
3526 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3527 op0 = convert (common_type, op0);
3528 op1 = convert (common_type, op1);
3529 }
3530
3531 /* Determine integer type to perform computations in. This will usually
3532 be the same as the result type (ptrdiff_t), but may need to be a wider
3533 type if pointers for the address space are wider than ptrdiff_t. */
3534 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3535 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3536 else
3537 inttype = restype;
3538
3539 if (TREE_CODE (target_type) == VOID_TYPE)
3540 pedwarn (loc, OPT_Wpointer_arith,
3541 "pointer of type %<void *%> used in subtraction");
3542 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3543 pedwarn (loc, OPT_Wpointer_arith,
3544 "pointer to a function used in subtraction");
3545
3546 /* First do the subtraction as integers;
3547 then drop through to build the divide operator.
3548 Do not do default conversions on the minus operator
3549 in case restype is a short type. */
3550
3551 op0 = build_binary_op (loc,
3552 MINUS_EXPR, convert (inttype, op0),
3553 convert (inttype, op1), 0);
3554 /* This generates an error if op1 is pointer to incomplete type. */
3555 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3556 error_at (loc, "arithmetic on pointer to an incomplete type");
3557
3558 op1 = c_size_in_bytes (target_type);
3559
3560 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3561 error_at (loc, "arithmetic on pointer to an empty aggregate");
3562
3563 /* Divide by the size, in easiest possible way. */
3564 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3565 op0, convert (inttype, op1));
3566
3567 /* Convert to final result type if necessary. */
3568 return convert (restype, result);
3569 }
3570 \f
3571 /* Expand atomic compound assignments into an approriate sequence as
3572 specified by the C11 standard section 6.5.16.2.
3573 given
3574 _Atomic T1 E1
3575 T2 E2
3576 E1 op= E2
3577
3578 This sequence is used for all types for which these operations are
3579 supported.
3580
3581 In addition, built-in versions of the 'fe' prefixed routines may
3582 need to be invoked for floating point (real, complex or vector) when
3583 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3584
3585 T1 newval;
3586 T1 old;
3587 T1 *addr
3588 T2 val
3589 fenv_t fenv
3590
3591 addr = &E1;
3592 val = (E2);
3593 __atomic_load (addr, &old, SEQ_CST);
3594 feholdexcept (&fenv);
3595 loop:
3596 newval = old op val;
3597 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3598 SEQ_CST))
3599 goto done;
3600 feclearexcept (FE_ALL_EXCEPT);
3601 goto loop:
3602 done:
3603 feupdateenv (&fenv);
3604
3605 Also note that the compiler is simply issuing the generic form of
3606 the atomic operations. This requires temp(s) and has their address
3607 taken. The atomic processing is smart enough to figure out when the
3608 size of an object can utilize a lock-free version, and convert the
3609 built-in call to the appropriate lock-free routine. The optimizers
3610 will then dispose of any temps that are no longer required, and
3611 lock-free implementations are utilized as long as there is target
3612 support for the required size.
3613
3614 If the operator is NOP_EXPR, then this is a simple assignment, and
3615 an __atomic_store is issued to perform the assignment rather than
3616 the above loop.
3617
3618 */
3619
3620 /* Build an atomic assignment at LOC, expanding into the proper
3621 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3622 the result of the operation, unless RETURN_OLD_P in which case
3623 return the old value of LHS (this is only for postincrement and
3624 postdecrement). */
3625 static tree
3626 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3627 tree rhs, bool return_old_p)
3628 {
3629 tree fndecl, func_call;
3630 vec<tree, va_gc> *params;
3631 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3632 tree old, old_addr;
3633 tree compound_stmt;
3634 tree stmt, goto_stmt;
3635 tree loop_label, loop_decl, done_label, done_decl;
3636
3637 tree lhs_type = TREE_TYPE (lhs);
3638 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3639 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3640 tree rhs_type = TREE_TYPE (rhs);
3641
3642 gcc_assert (TYPE_ATOMIC (lhs_type));
3643
3644 if (return_old_p)
3645 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3646
3647 /* Allocate enough vector items for a compare_exchange. */
3648 vec_alloc (params, 6);
3649
3650 /* Create a compound statement to hold the sequence of statements
3651 with a loop. */
3652 compound_stmt = c_begin_compound_stmt (false);
3653
3654 /* Fold the RHS if it hasn't already been folded. */
3655 if (modifycode != NOP_EXPR)
3656 rhs = c_fully_fold (rhs, false, NULL);
3657
3658 /* Remove the qualifiers for the rest of the expressions and create
3659 the VAL temp variable to hold the RHS. */
3660 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3661 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3662 val = create_tmp_var (nonatomic_rhs_type);
3663 TREE_ADDRESSABLE (val) = 1;
3664 TREE_NO_WARNING (val) = 1;
3665 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3666 SET_EXPR_LOCATION (rhs, loc);
3667 add_stmt (rhs);
3668
3669 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3670 an atomic_store. */
3671 if (modifycode == NOP_EXPR)
3672 {
3673 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3674 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3675 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3676 params->quick_push (lhs_addr);
3677 params->quick_push (rhs);
3678 params->quick_push (seq_cst);
3679 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3680 add_stmt (func_call);
3681
3682 /* Finish the compound statement. */
3683 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3684
3685 /* VAL is the value which was stored, return a COMPOUND_STMT of
3686 the statement and that value. */
3687 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3688 }
3689
3690 /* Create the variables and labels required for the op= form. */
3691 old = create_tmp_var (nonatomic_lhs_type);
3692 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3693 TREE_ADDRESSABLE (old) = 1;
3694 TREE_NO_WARNING (old) = 1;
3695
3696 newval = create_tmp_var (nonatomic_lhs_type);
3697 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3698 TREE_ADDRESSABLE (newval) = 1;
3699
3700 loop_decl = create_artificial_label (loc);
3701 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3702
3703 done_decl = create_artificial_label (loc);
3704 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3705
3706 /* __atomic_load (addr, &old, SEQ_CST). */
3707 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3708 params->quick_push (lhs_addr);
3709 params->quick_push (old_addr);
3710 params->quick_push (seq_cst);
3711 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3712 add_stmt (func_call);
3713 params->truncate (0);
3714
3715 /* Create the expressions for floating-point environment
3716 manipulation, if required. */
3717 bool need_fenv = (flag_trapping_math
3718 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3719 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3720 if (need_fenv)
3721 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3722
3723 if (hold_call)
3724 add_stmt (hold_call);
3725
3726 /* loop: */
3727 add_stmt (loop_label);
3728
3729 /* newval = old + val; */
3730 rhs = build_binary_op (loc, modifycode, old, val, 1);
3731 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3732 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3733 NULL_TREE, 0);
3734 if (rhs != error_mark_node)
3735 {
3736 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3737 SET_EXPR_LOCATION (rhs, loc);
3738 add_stmt (rhs);
3739 }
3740
3741 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3742 goto done; */
3743 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3744 params->quick_push (lhs_addr);
3745 params->quick_push (old_addr);
3746 params->quick_push (newval_addr);
3747 params->quick_push (integer_zero_node);
3748 params->quick_push (seq_cst);
3749 params->quick_push (seq_cst);
3750 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3751
3752 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3753 SET_EXPR_LOCATION (goto_stmt, loc);
3754
3755 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3756 SET_EXPR_LOCATION (stmt, loc);
3757 add_stmt (stmt);
3758
3759 if (clear_call)
3760 add_stmt (clear_call);
3761
3762 /* goto loop; */
3763 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3764 SET_EXPR_LOCATION (goto_stmt, loc);
3765 add_stmt (goto_stmt);
3766
3767 /* done: */
3768 add_stmt (done_label);
3769
3770 if (update_call)
3771 add_stmt (update_call);
3772
3773 /* Finish the compound statement. */
3774 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3775
3776 /* NEWVAL is the value that was successfully stored, return a
3777 COMPOUND_EXPR of the statement and the appropriate value. */
3778 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3779 return_old_p ? old : newval);
3780 }
3781
3782 /* Construct and perhaps optimize a tree representation
3783 for a unary operation. CODE, a tree_code, specifies the operation
3784 and XARG is the operand.
3785 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3786 the default promotions (such as from short to int).
3787 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3788 allows non-lvalues; this is only used to handle conversion of non-lvalue
3789 arrays to pointers in C99.
3790
3791 LOCATION is the location of the operator. */
3792
3793 tree
3794 build_unary_op (location_t location,
3795 enum tree_code code, tree xarg, int flag)
3796 {
3797 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3798 tree arg = xarg;
3799 tree argtype = 0;
3800 enum tree_code typecode;
3801 tree val;
3802 tree ret = error_mark_node;
3803 tree eptype = NULL_TREE;
3804 int noconvert = flag;
3805 const char *invalid_op_diag;
3806 bool int_operands;
3807
3808 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3809 if (int_operands)
3810 arg = remove_c_maybe_const_expr (arg);
3811
3812 if (code != ADDR_EXPR)
3813 arg = require_complete_type (arg);
3814
3815 typecode = TREE_CODE (TREE_TYPE (arg));
3816 if (typecode == ERROR_MARK)
3817 return error_mark_node;
3818 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3819 typecode = INTEGER_TYPE;
3820
3821 if ((invalid_op_diag
3822 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3823 {
3824 error_at (location, invalid_op_diag);
3825 return error_mark_node;
3826 }
3827
3828 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3829 {
3830 eptype = TREE_TYPE (arg);
3831 arg = TREE_OPERAND (arg, 0);
3832 }
3833
3834 switch (code)
3835 {
3836 case CONVERT_EXPR:
3837 /* This is used for unary plus, because a CONVERT_EXPR
3838 is enough to prevent anybody from looking inside for
3839 associativity, but won't generate any code. */
3840 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3841 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3842 || typecode == VECTOR_TYPE))
3843 {
3844 error_at (location, "wrong type argument to unary plus");
3845 return error_mark_node;
3846 }
3847 else if (!noconvert)
3848 arg = default_conversion (arg);
3849 arg = non_lvalue_loc (location, arg);
3850 break;
3851
3852 case NEGATE_EXPR:
3853 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3854 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3855 || typecode == VECTOR_TYPE))
3856 {
3857 error_at (location, "wrong type argument to unary minus");
3858 return error_mark_node;
3859 }
3860 else if (!noconvert)
3861 arg = default_conversion (arg);
3862 break;
3863
3864 case BIT_NOT_EXPR:
3865 /* ~ works on integer types and non float vectors. */
3866 if (typecode == INTEGER_TYPE
3867 || (typecode == VECTOR_TYPE
3868 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3869 {
3870 if (!noconvert)
3871 arg = default_conversion (arg);
3872 }
3873 else if (typecode == COMPLEX_TYPE)
3874 {
3875 code = CONJ_EXPR;
3876 pedwarn (location, OPT_Wpedantic,
3877 "ISO C does not support %<~%> for complex conjugation");
3878 if (!noconvert)
3879 arg = default_conversion (arg);
3880 }
3881 else
3882 {
3883 error_at (location, "wrong type argument to bit-complement");
3884 return error_mark_node;
3885 }
3886 break;
3887
3888 case ABS_EXPR:
3889 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3890 {
3891 error_at (location, "wrong type argument to abs");
3892 return error_mark_node;
3893 }
3894 else if (!noconvert)
3895 arg = default_conversion (arg);
3896 break;
3897
3898 case CONJ_EXPR:
3899 /* Conjugating a real value is a no-op, but allow it anyway. */
3900 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3901 || typecode == COMPLEX_TYPE))
3902 {
3903 error_at (location, "wrong type argument to conjugation");
3904 return error_mark_node;
3905 }
3906 else if (!noconvert)
3907 arg = default_conversion (arg);
3908 break;
3909
3910 case TRUTH_NOT_EXPR:
3911 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3912 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3913 && typecode != COMPLEX_TYPE)
3914 {
3915 error_at (location,
3916 "wrong type argument to unary exclamation mark");
3917 return error_mark_node;
3918 }
3919 if (int_operands)
3920 {
3921 arg = c_objc_common_truthvalue_conversion (location, xarg);
3922 arg = remove_c_maybe_const_expr (arg);
3923 }
3924 else
3925 arg = c_objc_common_truthvalue_conversion (location, arg);
3926 ret = invert_truthvalue_loc (location, arg);
3927 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3928 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3929 location = EXPR_LOCATION (ret);
3930 goto return_build_unary_op;
3931
3932 case REALPART_EXPR:
3933 case IMAGPART_EXPR:
3934 ret = build_real_imag_expr (location, code, arg);
3935 if (ret == error_mark_node)
3936 return error_mark_node;
3937 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3938 eptype = TREE_TYPE (eptype);
3939 goto return_build_unary_op;
3940
3941 case PREINCREMENT_EXPR:
3942 case POSTINCREMENT_EXPR:
3943 case PREDECREMENT_EXPR:
3944 case POSTDECREMENT_EXPR:
3945
3946 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3947 {
3948 tree inner = build_unary_op (location, code,
3949 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3950 if (inner == error_mark_node)
3951 return error_mark_node;
3952 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3953 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3954 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3955 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3956 goto return_build_unary_op;
3957 }
3958
3959 /* Complain about anything that is not a true lvalue. In
3960 Objective-C, skip this check for property_refs. */
3961 if (!objc_is_property_ref (arg)
3962 && !lvalue_or_else (location,
3963 arg, ((code == PREINCREMENT_EXPR
3964 || code == POSTINCREMENT_EXPR)
3965 ? lv_increment
3966 : lv_decrement)))
3967 return error_mark_node;
3968
3969 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3970 {
3971 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3972 warning_at (location, OPT_Wc___compat,
3973 "increment of enumeration value is invalid in C++");
3974 else
3975 warning_at (location, OPT_Wc___compat,
3976 "decrement of enumeration value is invalid in C++");
3977 }
3978
3979 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3980 arg = c_fully_fold (arg, false, NULL);
3981
3982 bool atomic_op;
3983 atomic_op = really_atomic_lvalue (arg);
3984
3985 /* Increment or decrement the real part of the value,
3986 and don't change the imaginary part. */
3987 if (typecode == COMPLEX_TYPE)
3988 {
3989 tree real, imag;
3990
3991 pedwarn (location, OPT_Wpedantic,
3992 "ISO C does not support %<++%> and %<--%> on complex types");
3993
3994 if (!atomic_op)
3995 {
3996 arg = stabilize_reference (arg);
3997 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3998 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3999 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4000 if (real == error_mark_node || imag == error_mark_node)
4001 return error_mark_node;
4002 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4003 real, imag);
4004 goto return_build_unary_op;
4005 }
4006 }
4007
4008 /* Report invalid types. */
4009
4010 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4011 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4012 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4013 {
4014 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4015 error_at (location, "wrong type argument to increment");
4016 else
4017 error_at (location, "wrong type argument to decrement");
4018
4019 return error_mark_node;
4020 }
4021
4022 {
4023 tree inc;
4024
4025 argtype = TREE_TYPE (arg);
4026
4027 /* Compute the increment. */
4028
4029 if (typecode == POINTER_TYPE)
4030 {
4031 /* If pointer target is an incomplete type,
4032 we just cannot know how to do the arithmetic. */
4033 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4034 {
4035 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4036 error_at (location,
4037 "increment of pointer to an incomplete type %qT",
4038 TREE_TYPE (argtype));
4039 else
4040 error_at (location,
4041 "decrement of pointer to an incomplete type %qT",
4042 TREE_TYPE (argtype));
4043 }
4044 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4045 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4046 {
4047 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4048 pedwarn (location, OPT_Wpointer_arith,
4049 "wrong type argument to increment");
4050 else
4051 pedwarn (location, OPT_Wpointer_arith,
4052 "wrong type argument to decrement");
4053 }
4054
4055 inc = c_size_in_bytes (TREE_TYPE (argtype));
4056 inc = convert_to_ptrofftype_loc (location, inc);
4057 }
4058 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4059 {
4060 /* For signed fract types, we invert ++ to -- or
4061 -- to ++, and change inc from 1 to -1, because
4062 it is not possible to represent 1 in signed fract constants.
4063 For unsigned fract types, the result always overflows and
4064 we get an undefined (original) or the maximum value. */
4065 if (code == PREINCREMENT_EXPR)
4066 code = PREDECREMENT_EXPR;
4067 else if (code == PREDECREMENT_EXPR)
4068 code = PREINCREMENT_EXPR;
4069 else if (code == POSTINCREMENT_EXPR)
4070 code = POSTDECREMENT_EXPR;
4071 else /* code == POSTDECREMENT_EXPR */
4072 code = POSTINCREMENT_EXPR;
4073
4074 inc = integer_minus_one_node;
4075 inc = convert (argtype, inc);
4076 }
4077 else
4078 {
4079 inc = VECTOR_TYPE_P (argtype)
4080 ? build_one_cst (argtype)
4081 : integer_one_node;
4082 inc = convert (argtype, inc);
4083 }
4084
4085 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4086 need to ask Objective-C to build the increment or decrement
4087 expression for it. */
4088 if (objc_is_property_ref (arg))
4089 return objc_build_incr_expr_for_property_ref (location, code,
4090 arg, inc);
4091
4092 /* Report a read-only lvalue. */
4093 if (TYPE_READONLY (argtype))
4094 {
4095 readonly_error (location, arg,
4096 ((code == PREINCREMENT_EXPR
4097 || code == POSTINCREMENT_EXPR)
4098 ? lv_increment : lv_decrement));
4099 return error_mark_node;
4100 }
4101 else if (TREE_READONLY (arg))
4102 readonly_warning (arg,
4103 ((code == PREINCREMENT_EXPR
4104 || code == POSTINCREMENT_EXPR)
4105 ? lv_increment : lv_decrement));
4106
4107 /* If the argument is atomic, use the special code sequences for
4108 atomic compound assignment. */
4109 if (atomic_op)
4110 {
4111 arg = stabilize_reference (arg);
4112 ret = build_atomic_assign (location, arg,
4113 ((code == PREINCREMENT_EXPR
4114 || code == POSTINCREMENT_EXPR)
4115 ? PLUS_EXPR
4116 : MINUS_EXPR),
4117 (FRACT_MODE_P (TYPE_MODE (argtype))
4118 ? inc
4119 : integer_one_node),
4120 (code == POSTINCREMENT_EXPR
4121 || code == POSTDECREMENT_EXPR));
4122 goto return_build_unary_op;
4123 }
4124
4125 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4126 val = boolean_increment (code, arg);
4127 else
4128 val = build2 (code, TREE_TYPE (arg), arg, inc);
4129 TREE_SIDE_EFFECTS (val) = 1;
4130 if (TREE_CODE (val) != code)
4131 TREE_NO_WARNING (val) = 1;
4132 ret = val;
4133 goto return_build_unary_op;
4134 }
4135
4136 case ADDR_EXPR:
4137 /* Note that this operation never does default_conversion. */
4138
4139 /* The operand of unary '&' must be an lvalue (which excludes
4140 expressions of type void), or, in C99, the result of a [] or
4141 unary '*' operator. */
4142 if (VOID_TYPE_P (TREE_TYPE (arg))
4143 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4144 && (TREE_CODE (arg) != INDIRECT_REF
4145 || !flag_isoc99))
4146 pedwarn (location, 0, "taking address of expression of type %<void%>");
4147
4148 /* Let &* cancel out to simplify resulting code. */
4149 if (TREE_CODE (arg) == INDIRECT_REF)
4150 {
4151 /* Don't let this be an lvalue. */
4152 if (lvalue_p (TREE_OPERAND (arg, 0)))
4153 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4154 ret = TREE_OPERAND (arg, 0);
4155 goto return_build_unary_op;
4156 }
4157
4158 /* For &x[y], return x+y */
4159 if (TREE_CODE (arg) == ARRAY_REF)
4160 {
4161 tree op0 = TREE_OPERAND (arg, 0);
4162 if (!c_mark_addressable (op0))
4163 return error_mark_node;
4164 }
4165
4166 /* Anything not already handled and not a true memory reference
4167 or a non-lvalue array is an error. */
4168 else if (typecode != FUNCTION_TYPE && !flag
4169 && !lvalue_or_else (location, arg, lv_addressof))
4170 return error_mark_node;
4171
4172 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4173 folding later. */
4174 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4175 {
4176 tree inner = build_unary_op (location, code,
4177 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4178 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4179 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4180 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4181 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4182 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4183 goto return_build_unary_op;
4184 }
4185
4186 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4187 argtype = TREE_TYPE (arg);
4188
4189 /* If the lvalue is const or volatile, merge that into the type
4190 to which the address will point. This is only needed
4191 for function types. */
4192 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4193 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4194 && TREE_CODE (argtype) == FUNCTION_TYPE)
4195 {
4196 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4197 int quals = orig_quals;
4198
4199 if (TREE_READONLY (arg))
4200 quals |= TYPE_QUAL_CONST;
4201 if (TREE_THIS_VOLATILE (arg))
4202 quals |= TYPE_QUAL_VOLATILE;
4203
4204 argtype = c_build_qualified_type (argtype, quals);
4205 }
4206
4207 if (!c_mark_addressable (arg))
4208 return error_mark_node;
4209
4210 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4211 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4212
4213 argtype = build_pointer_type (argtype);
4214
4215 /* ??? Cope with user tricks that amount to offsetof. Delete this
4216 when we have proper support for integer constant expressions. */
4217 val = get_base_address (arg);
4218 if (val && TREE_CODE (val) == INDIRECT_REF
4219 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4220 {
4221 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4222 goto return_build_unary_op;
4223 }
4224
4225 val = build1 (ADDR_EXPR, argtype, arg);
4226
4227 ret = val;
4228 goto return_build_unary_op;
4229
4230 default:
4231 gcc_unreachable ();
4232 }
4233
4234 if (argtype == 0)
4235 argtype = TREE_TYPE (arg);
4236 if (TREE_CODE (arg) == INTEGER_CST)
4237 ret = (require_constant_value
4238 ? fold_build1_initializer_loc (location, code, argtype, arg)
4239 : fold_build1_loc (location, code, argtype, arg));
4240 else
4241 ret = build1 (code, argtype, arg);
4242 return_build_unary_op:
4243 gcc_assert (ret != error_mark_node);
4244 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4245 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4246 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4247 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4248 ret = note_integer_operands (ret);
4249 if (eptype)
4250 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4251 protected_set_expr_location (ret, location);
4252 return ret;
4253 }
4254
4255 /* Return nonzero if REF is an lvalue valid for this language.
4256 Lvalues can be assigned, unless their type has TYPE_READONLY.
4257 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4258
4259 bool
4260 lvalue_p (const_tree ref)
4261 {
4262 const enum tree_code code = TREE_CODE (ref);
4263
4264 switch (code)
4265 {
4266 case REALPART_EXPR:
4267 case IMAGPART_EXPR:
4268 case COMPONENT_REF:
4269 return lvalue_p (TREE_OPERAND (ref, 0));
4270
4271 case C_MAYBE_CONST_EXPR:
4272 return lvalue_p (TREE_OPERAND (ref, 1));
4273
4274 case COMPOUND_LITERAL_EXPR:
4275 case STRING_CST:
4276 return 1;
4277
4278 case INDIRECT_REF:
4279 case ARRAY_REF:
4280 case ARRAY_NOTATION_REF:
4281 case VAR_DECL:
4282 case PARM_DECL:
4283 case RESULT_DECL:
4284 case ERROR_MARK:
4285 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4286 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4287
4288 case BIND_EXPR:
4289 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4290
4291 default:
4292 return 0;
4293 }
4294 }
4295 \f
4296 /* Give a warning for storing in something that is read-only in GCC
4297 terms but not const in ISO C terms. */
4298
4299 static void
4300 readonly_warning (tree arg, enum lvalue_use use)
4301 {
4302 switch (use)
4303 {
4304 case lv_assign:
4305 warning (0, "assignment of read-only location %qE", arg);
4306 break;
4307 case lv_increment:
4308 warning (0, "increment of read-only location %qE", arg);
4309 break;
4310 case lv_decrement:
4311 warning (0, "decrement of read-only location %qE", arg);
4312 break;
4313 default:
4314 gcc_unreachable ();
4315 }
4316 return;
4317 }
4318
4319
4320 /* Return nonzero if REF is an lvalue valid for this language;
4321 otherwise, print an error message and return zero. USE says
4322 how the lvalue is being used and so selects the error message.
4323 LOCATION is the location at which any error should be reported. */
4324
4325 static int
4326 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4327 {
4328 int win = lvalue_p (ref);
4329
4330 if (!win)
4331 lvalue_error (loc, use);
4332
4333 return win;
4334 }
4335 \f
4336 /* Mark EXP saying that we need to be able to take the
4337 address of it; it should not be allocated in a register.
4338 Returns true if successful. */
4339
4340 bool
4341 c_mark_addressable (tree exp)
4342 {
4343 tree x = exp;
4344
4345 while (1)
4346 switch (TREE_CODE (x))
4347 {
4348 case COMPONENT_REF:
4349 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4350 {
4351 error
4352 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4353 return false;
4354 }
4355
4356 /* ... fall through ... */
4357
4358 case ADDR_EXPR:
4359 case ARRAY_REF:
4360 case REALPART_EXPR:
4361 case IMAGPART_EXPR:
4362 x = TREE_OPERAND (x, 0);
4363 break;
4364
4365 case COMPOUND_LITERAL_EXPR:
4366 case CONSTRUCTOR:
4367 TREE_ADDRESSABLE (x) = 1;
4368 return true;
4369
4370 case VAR_DECL:
4371 case CONST_DECL:
4372 case PARM_DECL:
4373 case RESULT_DECL:
4374 if (C_DECL_REGISTER (x)
4375 && DECL_NONLOCAL (x))
4376 {
4377 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4378 {
4379 error
4380 ("global register variable %qD used in nested function", x);
4381 return false;
4382 }
4383 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4384 }
4385 else if (C_DECL_REGISTER (x))
4386 {
4387 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4388 error ("address of global register variable %qD requested", x);
4389 else
4390 error ("address of register variable %qD requested", x);
4391 return false;
4392 }
4393
4394 /* drops in */
4395 case FUNCTION_DECL:
4396 TREE_ADDRESSABLE (x) = 1;
4397 /* drops out */
4398 default:
4399 return true;
4400 }
4401 }
4402 \f
4403 /* Convert EXPR to TYPE, warning about conversion problems with
4404 constants. SEMANTIC_TYPE is the type this conversion would use
4405 without excess precision. If SEMANTIC_TYPE is NULL, this function
4406 is equivalent to convert_and_check. This function is a wrapper that
4407 handles conversions that may be different than
4408 the usual ones because of excess precision. */
4409
4410 static tree
4411 ep_convert_and_check (location_t loc, tree type, tree expr,
4412 tree semantic_type)
4413 {
4414 if (TREE_TYPE (expr) == type)
4415 return expr;
4416
4417 if (!semantic_type)
4418 return convert_and_check (loc, type, expr);
4419
4420 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4421 && TREE_TYPE (expr) != semantic_type)
4422 {
4423 /* For integers, we need to check the real conversion, not
4424 the conversion to the excess precision type. */
4425 expr = convert_and_check (loc, semantic_type, expr);
4426 }
4427 /* Result type is the excess precision type, which should be
4428 large enough, so do not check. */
4429 return convert (type, expr);
4430 }
4431
4432 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4433 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4434 if folded to an integer constant then the unselected half may
4435 contain arbitrary operations not normally permitted in constant
4436 expressions. Set the location of the expression to LOC. */
4437
4438 tree
4439 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4440 tree op1, tree op1_original_type, tree op2,
4441 tree op2_original_type)
4442 {
4443 tree type1;
4444 tree type2;
4445 enum tree_code code1;
4446 enum tree_code code2;
4447 tree result_type = NULL;
4448 tree semantic_result_type = NULL;
4449 tree orig_op1 = op1, orig_op2 = op2;
4450 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4451 bool ifexp_int_operands;
4452 tree ret;
4453
4454 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4455 if (op1_int_operands)
4456 op1 = remove_c_maybe_const_expr (op1);
4457 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4458 if (op2_int_operands)
4459 op2 = remove_c_maybe_const_expr (op2);
4460 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4461 if (ifexp_int_operands)
4462 ifexp = remove_c_maybe_const_expr (ifexp);
4463
4464 /* Promote both alternatives. */
4465
4466 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4467 op1 = default_conversion (op1);
4468 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4469 op2 = default_conversion (op2);
4470
4471 if (TREE_CODE (ifexp) == ERROR_MARK
4472 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4473 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4474 return error_mark_node;
4475
4476 type1 = TREE_TYPE (op1);
4477 code1 = TREE_CODE (type1);
4478 type2 = TREE_TYPE (op2);
4479 code2 = TREE_CODE (type2);
4480
4481 /* C90 does not permit non-lvalue arrays in conditional expressions.
4482 In C99 they will be pointers by now. */
4483 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4484 {
4485 error_at (colon_loc, "non-lvalue array in conditional expression");
4486 return error_mark_node;
4487 }
4488
4489 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4490 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4491 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4492 || code1 == COMPLEX_TYPE)
4493 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4494 || code2 == COMPLEX_TYPE))
4495 {
4496 semantic_result_type = c_common_type (type1, type2);
4497 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4498 {
4499 op1 = TREE_OPERAND (op1, 0);
4500 type1 = TREE_TYPE (op1);
4501 gcc_assert (TREE_CODE (type1) == code1);
4502 }
4503 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4504 {
4505 op2 = TREE_OPERAND (op2, 0);
4506 type2 = TREE_TYPE (op2);
4507 gcc_assert (TREE_CODE (type2) == code2);
4508 }
4509 }
4510
4511 if (warn_cxx_compat)
4512 {
4513 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4514 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4515
4516 if (TREE_CODE (t1) == ENUMERAL_TYPE
4517 && TREE_CODE (t2) == ENUMERAL_TYPE
4518 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4519 warning_at (colon_loc, OPT_Wc___compat,
4520 ("different enum types in conditional is "
4521 "invalid in C++: %qT vs %qT"),
4522 t1, t2);
4523 }
4524
4525 /* Quickly detect the usual case where op1 and op2 have the same type
4526 after promotion. */
4527 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4528 {
4529 if (type1 == type2)
4530 result_type = type1;
4531 else
4532 result_type = TYPE_MAIN_VARIANT (type1);
4533 }
4534 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4535 || code1 == COMPLEX_TYPE)
4536 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4537 || code2 == COMPLEX_TYPE))
4538 {
4539 result_type = c_common_type (type1, type2);
4540 do_warn_double_promotion (result_type, type1, type2,
4541 "implicit conversion from %qT to %qT to "
4542 "match other result of conditional",
4543 colon_loc);
4544
4545 /* If -Wsign-compare, warn here if type1 and type2 have
4546 different signedness. We'll promote the signed to unsigned
4547 and later code won't know it used to be different.
4548 Do this check on the original types, so that explicit casts
4549 will be considered, but default promotions won't. */
4550 if (c_inhibit_evaluation_warnings == 0)
4551 {
4552 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4553 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4554
4555 if (unsigned_op1 ^ unsigned_op2)
4556 {
4557 bool ovf;
4558
4559 /* Do not warn if the result type is signed, since the
4560 signed type will only be chosen if it can represent
4561 all the values of the unsigned type. */
4562 if (!TYPE_UNSIGNED (result_type))
4563 /* OK */;
4564 else
4565 {
4566 bool op1_maybe_const = true;
4567 bool op2_maybe_const = true;
4568
4569 /* Do not warn if the signed quantity is an
4570 unsuffixed integer literal (or some static
4571 constant expression involving such literals) and
4572 it is non-negative. This warning requires the
4573 operands to be folded for best results, so do
4574 that folding in this case even without
4575 warn_sign_compare to avoid warning options
4576 possibly affecting code generation. */
4577 c_inhibit_evaluation_warnings
4578 += (ifexp == truthvalue_false_node);
4579 op1 = c_fully_fold (op1, require_constant_value,
4580 &op1_maybe_const);
4581 c_inhibit_evaluation_warnings
4582 -= (ifexp == truthvalue_false_node);
4583
4584 c_inhibit_evaluation_warnings
4585 += (ifexp == truthvalue_true_node);
4586 op2 = c_fully_fold (op2, require_constant_value,
4587 &op2_maybe_const);
4588 c_inhibit_evaluation_warnings
4589 -= (ifexp == truthvalue_true_node);
4590
4591 if (warn_sign_compare)
4592 {
4593 if ((unsigned_op2
4594 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4595 || (unsigned_op1
4596 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4597 /* OK */;
4598 else
4599 warning_at (colon_loc, OPT_Wsign_compare,
4600 ("signed and unsigned type in "
4601 "conditional expression"));
4602 }
4603 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4604 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4605 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4606 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4607 }
4608 }
4609 }
4610 }
4611 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4612 {
4613 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4614 pedwarn (colon_loc, OPT_Wpedantic,
4615 "ISO C forbids conditional expr with only one void side");
4616 result_type = void_type_node;
4617 }
4618 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4619 {
4620 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4621 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4622 addr_space_t as_common;
4623
4624 if (comp_target_types (colon_loc, type1, type2))
4625 result_type = common_pointer_type (type1, type2);
4626 else if (null_pointer_constant_p (orig_op1))
4627 result_type = type2;
4628 else if (null_pointer_constant_p (orig_op2))
4629 result_type = type1;
4630 else if (!addr_space_superset (as1, as2, &as_common))
4631 {
4632 error_at (colon_loc, "pointers to disjoint address spaces "
4633 "used in conditional expression");
4634 return error_mark_node;
4635 }
4636 else if (VOID_TYPE_P (TREE_TYPE (type1))
4637 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4638 {
4639 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4640 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4641 & ~TYPE_QUALS (TREE_TYPE (type1))))
4642 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4643 "pointer to array loses qualifier "
4644 "in conditional expression");
4645
4646 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4647 pedwarn (colon_loc, OPT_Wpedantic,
4648 "ISO C forbids conditional expr between "
4649 "%<void *%> and function pointer");
4650 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4651 TREE_TYPE (type2)));
4652 }
4653 else if (VOID_TYPE_P (TREE_TYPE (type2))
4654 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4655 {
4656 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4657 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4658 & ~TYPE_QUALS (TREE_TYPE (type2))))
4659 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4660 "pointer to array loses qualifier "
4661 "in conditional expression");
4662
4663 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4664 pedwarn (colon_loc, OPT_Wpedantic,
4665 "ISO C forbids conditional expr between "
4666 "%<void *%> and function pointer");
4667 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4668 TREE_TYPE (type1)));
4669 }
4670 /* Objective-C pointer comparisons are a bit more lenient. */
4671 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4672 result_type = objc_common_type (type1, type2);
4673 else
4674 {
4675 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4676
4677 pedwarn (colon_loc, 0,
4678 "pointer type mismatch in conditional expression");
4679 result_type = build_pointer_type
4680 (build_qualified_type (void_type_node, qual));
4681 }
4682 }
4683 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4684 {
4685 if (!null_pointer_constant_p (orig_op2))
4686 pedwarn (colon_loc, 0,
4687 "pointer/integer type mismatch in conditional expression");
4688 else
4689 {
4690 op2 = null_pointer_node;
4691 }
4692 result_type = type1;
4693 }
4694 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4695 {
4696 if (!null_pointer_constant_p (orig_op1))
4697 pedwarn (colon_loc, 0,
4698 "pointer/integer type mismatch in conditional expression");
4699 else
4700 {
4701 op1 = null_pointer_node;
4702 }
4703 result_type = type2;
4704 }
4705
4706 if (!result_type)
4707 {
4708 if (flag_cond_mismatch)
4709 result_type = void_type_node;
4710 else
4711 {
4712 error_at (colon_loc, "type mismatch in conditional expression");
4713 return error_mark_node;
4714 }
4715 }
4716
4717 /* Merge const and volatile flags of the incoming types. */
4718 result_type
4719 = build_type_variant (result_type,
4720 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4721 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4722
4723 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4724 semantic_result_type);
4725 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4726 semantic_result_type);
4727
4728 if (ifexp_bcp && ifexp == truthvalue_true_node)
4729 {
4730 op2_int_operands = true;
4731 op1 = c_fully_fold (op1, require_constant_value, NULL);
4732 }
4733 if (ifexp_bcp && ifexp == truthvalue_false_node)
4734 {
4735 op1_int_operands = true;
4736 op2 = c_fully_fold (op2, require_constant_value, NULL);
4737 }
4738 int_const = int_operands = (ifexp_int_operands
4739 && op1_int_operands
4740 && op2_int_operands);
4741 if (int_operands)
4742 {
4743 int_const = ((ifexp == truthvalue_true_node
4744 && TREE_CODE (orig_op1) == INTEGER_CST
4745 && !TREE_OVERFLOW (orig_op1))
4746 || (ifexp == truthvalue_false_node
4747 && TREE_CODE (orig_op2) == INTEGER_CST
4748 && !TREE_OVERFLOW (orig_op2)));
4749 }
4750 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4751 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4752 else
4753 {
4754 if (int_operands)
4755 {
4756 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4757 nested inside of the expression. */
4758 op1 = c_fully_fold (op1, false, NULL);
4759 op2 = c_fully_fold (op2, false, NULL);
4760 }
4761 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4762 if (int_operands)
4763 ret = note_integer_operands (ret);
4764 }
4765 if (semantic_result_type)
4766 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4767
4768 protected_set_expr_location (ret, colon_loc);
4769 return ret;
4770 }
4771 \f
4772 /* Return a compound expression that performs two expressions and
4773 returns the value of the second of them.
4774
4775 LOC is the location of the COMPOUND_EXPR. */
4776
4777 tree
4778 build_compound_expr (location_t loc, tree expr1, tree expr2)
4779 {
4780 bool expr1_int_operands, expr2_int_operands;
4781 tree eptype = NULL_TREE;
4782 tree ret;
4783
4784 if (flag_cilkplus
4785 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4786 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4787 {
4788 error_at (loc,
4789 "spawned function call cannot be part of a comma expression");
4790 return error_mark_node;
4791 }
4792 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4793 if (expr1_int_operands)
4794 expr1 = remove_c_maybe_const_expr (expr1);
4795 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4796 if (expr2_int_operands)
4797 expr2 = remove_c_maybe_const_expr (expr2);
4798
4799 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4800 expr1 = TREE_OPERAND (expr1, 0);
4801 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4802 {
4803 eptype = TREE_TYPE (expr2);
4804 expr2 = TREE_OPERAND (expr2, 0);
4805 }
4806
4807 if (!TREE_SIDE_EFFECTS (expr1))
4808 {
4809 /* The left-hand operand of a comma expression is like an expression
4810 statement: with -Wunused, we should warn if it doesn't have
4811 any side-effects, unless it was explicitly cast to (void). */
4812 if (warn_unused_value)
4813 {
4814 if (VOID_TYPE_P (TREE_TYPE (expr1))
4815 && CONVERT_EXPR_P (expr1))
4816 ; /* (void) a, b */
4817 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4818 && TREE_CODE (expr1) == COMPOUND_EXPR
4819 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4820 ; /* (void) a, (void) b, c */
4821 else
4822 warning_at (loc, OPT_Wunused_value,
4823 "left-hand operand of comma expression has no effect");
4824 }
4825 }
4826 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4827 && warn_unused_value)
4828 {
4829 tree r = expr1;
4830 location_t cloc = loc;
4831 while (TREE_CODE (r) == COMPOUND_EXPR)
4832 {
4833 if (EXPR_HAS_LOCATION (r))
4834 cloc = EXPR_LOCATION (r);
4835 r = TREE_OPERAND (r, 1);
4836 }
4837 if (!TREE_SIDE_EFFECTS (r)
4838 && !VOID_TYPE_P (TREE_TYPE (r))
4839 && !CONVERT_EXPR_P (r))
4840 warning_at (cloc, OPT_Wunused_value,
4841 "right-hand operand of comma expression has no effect");
4842 }
4843
4844 /* With -Wunused, we should also warn if the left-hand operand does have
4845 side-effects, but computes a value which is not used. For example, in
4846 `foo() + bar(), baz()' the result of the `+' operator is not used,
4847 so we should issue a warning. */
4848 else if (warn_unused_value)
4849 warn_if_unused_value (expr1, loc);
4850
4851 if (expr2 == error_mark_node)
4852 return error_mark_node;
4853
4854 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4855
4856 if (flag_isoc99
4857 && expr1_int_operands
4858 && expr2_int_operands)
4859 ret = note_integer_operands (ret);
4860
4861 if (eptype)
4862 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4863
4864 protected_set_expr_location (ret, loc);
4865 return ret;
4866 }
4867
4868 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4869 which we are casting. OTYPE is the type of the expression being
4870 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4871 of the cast. -Wcast-qual appeared on the command line. Named
4872 address space qualifiers are not handled here, because they result
4873 in different warnings. */
4874
4875 static void
4876 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4877 {
4878 tree in_type = type;
4879 tree in_otype = otype;
4880 int added = 0;
4881 int discarded = 0;
4882 bool is_const;
4883
4884 /* Check that the qualifiers on IN_TYPE are a superset of the
4885 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4886 nodes is uninteresting and we stop as soon as we hit a
4887 non-POINTER_TYPE node on either type. */
4888 do
4889 {
4890 in_otype = TREE_TYPE (in_otype);
4891 in_type = TREE_TYPE (in_type);
4892
4893 /* GNU C allows cv-qualified function types. 'const' means the
4894 function is very pure, 'volatile' means it can't return. We
4895 need to warn when such qualifiers are added, not when they're
4896 taken away. */
4897 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4898 && TREE_CODE (in_type) == FUNCTION_TYPE)
4899 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4900 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4901 else
4902 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4903 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4904 }
4905 while (TREE_CODE (in_type) == POINTER_TYPE
4906 && TREE_CODE (in_otype) == POINTER_TYPE);
4907
4908 if (added)
4909 warning_at (loc, OPT_Wcast_qual,
4910 "cast adds %q#v qualifier to function type", added);
4911
4912 if (discarded)
4913 /* There are qualifiers present in IN_OTYPE that are not present
4914 in IN_TYPE. */
4915 warning_at (loc, OPT_Wcast_qual,
4916 "cast discards %qv qualifier from pointer target type",
4917 discarded);
4918
4919 if (added || discarded)
4920 return;
4921
4922 /* A cast from **T to const **T is unsafe, because it can cause a
4923 const value to be changed with no additional warning. We only
4924 issue this warning if T is the same on both sides, and we only
4925 issue the warning if there are the same number of pointers on
4926 both sides, as otherwise the cast is clearly unsafe anyhow. A
4927 cast is unsafe when a qualifier is added at one level and const
4928 is not present at all outer levels.
4929
4930 To issue this warning, we check at each level whether the cast
4931 adds new qualifiers not already seen. We don't need to special
4932 case function types, as they won't have the same
4933 TYPE_MAIN_VARIANT. */
4934
4935 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4936 return;
4937 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4938 return;
4939
4940 in_type = type;
4941 in_otype = otype;
4942 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4943 do
4944 {
4945 in_type = TREE_TYPE (in_type);
4946 in_otype = TREE_TYPE (in_otype);
4947 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4948 && !is_const)
4949 {
4950 warning_at (loc, OPT_Wcast_qual,
4951 "to be safe all intermediate pointers in cast from "
4952 "%qT to %qT must be %<const%> qualified",
4953 otype, type);
4954 break;
4955 }
4956 if (is_const)
4957 is_const = TYPE_READONLY (in_type);
4958 }
4959 while (TREE_CODE (in_type) == POINTER_TYPE);
4960 }
4961
4962 /* Build an expression representing a cast to type TYPE of expression EXPR.
4963 LOC is the location of the cast-- typically the open paren of the cast. */
4964
4965 tree
4966 build_c_cast (location_t loc, tree type, tree expr)
4967 {
4968 tree value;
4969
4970 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4971 expr = TREE_OPERAND (expr, 0);
4972
4973 value = expr;
4974
4975 if (type == error_mark_node || expr == error_mark_node)
4976 return error_mark_node;
4977
4978 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4979 only in <protocol> qualifications. But when constructing cast expressions,
4980 the protocols do matter and must be kept around. */
4981 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4982 return build1 (NOP_EXPR, type, expr);
4983
4984 type = TYPE_MAIN_VARIANT (type);
4985
4986 if (TREE_CODE (type) == ARRAY_TYPE)
4987 {
4988 error_at (loc, "cast specifies array type");
4989 return error_mark_node;
4990 }
4991
4992 if (TREE_CODE (type) == FUNCTION_TYPE)
4993 {
4994 error_at (loc, "cast specifies function type");
4995 return error_mark_node;
4996 }
4997
4998 if (!VOID_TYPE_P (type))
4999 {
5000 value = require_complete_type (value);
5001 if (value == error_mark_node)
5002 return error_mark_node;
5003 }
5004
5005 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5006 {
5007 if (TREE_CODE (type) == RECORD_TYPE
5008 || TREE_CODE (type) == UNION_TYPE)
5009 pedwarn (loc, OPT_Wpedantic,
5010 "ISO C forbids casting nonscalar to the same type");
5011
5012 /* Convert to remove any qualifiers from VALUE's type. */
5013 value = convert (type, value);
5014 }
5015 else if (TREE_CODE (type) == UNION_TYPE)
5016 {
5017 tree field;
5018
5019 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5020 if (TREE_TYPE (field) != error_mark_node
5021 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5022 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5023 break;
5024
5025 if (field)
5026 {
5027 tree t;
5028 bool maybe_const = true;
5029
5030 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5031 t = c_fully_fold (value, false, &maybe_const);
5032 t = build_constructor_single (type, field, t);
5033 if (!maybe_const)
5034 t = c_wrap_maybe_const (t, true);
5035 t = digest_init (loc, type, t,
5036 NULL_TREE, false, true, 0);
5037 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5038 return t;
5039 }
5040 error_at (loc, "cast to union type from type not present in union");
5041 return error_mark_node;
5042 }
5043 else
5044 {
5045 tree otype, ovalue;
5046
5047 if (type == void_type_node)
5048 {
5049 tree t = build1 (CONVERT_EXPR, type, value);
5050 SET_EXPR_LOCATION (t, loc);
5051 return t;
5052 }
5053
5054 otype = TREE_TYPE (value);
5055
5056 /* Optionally warn about potentially worrisome casts. */
5057 if (warn_cast_qual
5058 && TREE_CODE (type) == POINTER_TYPE
5059 && TREE_CODE (otype) == POINTER_TYPE)
5060 handle_warn_cast_qual (loc, type, otype);
5061
5062 /* Warn about conversions between pointers to disjoint
5063 address spaces. */
5064 if (TREE_CODE (type) == POINTER_TYPE
5065 && TREE_CODE (otype) == POINTER_TYPE
5066 && !null_pointer_constant_p (value))
5067 {
5068 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5069 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5070 addr_space_t as_common;
5071
5072 if (!addr_space_superset (as_to, as_from, &as_common))
5073 {
5074 if (ADDR_SPACE_GENERIC_P (as_from))
5075 warning_at (loc, 0, "cast to %s address space pointer "
5076 "from disjoint generic address space pointer",
5077 c_addr_space_name (as_to));
5078
5079 else if (ADDR_SPACE_GENERIC_P (as_to))
5080 warning_at (loc, 0, "cast to generic address space pointer "
5081 "from disjoint %s address space pointer",
5082 c_addr_space_name (as_from));
5083
5084 else
5085 warning_at (loc, 0, "cast to %s address space pointer "
5086 "from disjoint %s address space pointer",
5087 c_addr_space_name (as_to),
5088 c_addr_space_name (as_from));
5089 }
5090 }
5091
5092 /* Warn about possible alignment problems. */
5093 if (STRICT_ALIGNMENT
5094 && TREE_CODE (type) == POINTER_TYPE
5095 && TREE_CODE (otype) == POINTER_TYPE
5096 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5097 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5098 /* Don't warn about opaque types, where the actual alignment
5099 restriction is unknown. */
5100 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5101 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5102 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5103 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5104 warning_at (loc, OPT_Wcast_align,
5105 "cast increases required alignment of target type");
5106
5107 if (TREE_CODE (type) == INTEGER_TYPE
5108 && TREE_CODE (otype) == POINTER_TYPE
5109 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5110 /* Unlike conversion of integers to pointers, where the
5111 warning is disabled for converting constants because
5112 of cases such as SIG_*, warn about converting constant
5113 pointers to integers. In some cases it may cause unwanted
5114 sign extension, and a warning is appropriate. */
5115 warning_at (loc, OPT_Wpointer_to_int_cast,
5116 "cast from pointer to integer of different size");
5117
5118 if (TREE_CODE (value) == CALL_EXPR
5119 && TREE_CODE (type) != TREE_CODE (otype))
5120 warning_at (loc, OPT_Wbad_function_cast,
5121 "cast from function call of type %qT "
5122 "to non-matching type %qT", otype, type);
5123
5124 if (TREE_CODE (type) == POINTER_TYPE
5125 && TREE_CODE (otype) == INTEGER_TYPE
5126 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5127 /* Don't warn about converting any constant. */
5128 && !TREE_CONSTANT (value))
5129 warning_at (loc,
5130 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5131 "of different size");
5132
5133 if (warn_strict_aliasing <= 2)
5134 strict_aliasing_warning (otype, type, expr);
5135
5136 /* If pedantic, warn for conversions between function and object
5137 pointer types, except for converting a null pointer constant
5138 to function pointer type. */
5139 if (pedantic
5140 && TREE_CODE (type) == POINTER_TYPE
5141 && TREE_CODE (otype) == POINTER_TYPE
5142 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5143 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5144 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5145 "conversion of function pointer to object pointer type");
5146
5147 if (pedantic
5148 && TREE_CODE (type) == POINTER_TYPE
5149 && TREE_CODE (otype) == POINTER_TYPE
5150 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5151 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5152 && !null_pointer_constant_p (value))
5153 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5154 "conversion of object pointer to function pointer type");
5155
5156 ovalue = value;
5157 value = convert (type, value);
5158
5159 /* Ignore any integer overflow caused by the cast. */
5160 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5161 {
5162 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5163 {
5164 if (!TREE_OVERFLOW (value))
5165 {
5166 /* Avoid clobbering a shared constant. */
5167 value = copy_node (value);
5168 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5169 }
5170 }
5171 else if (TREE_OVERFLOW (value))
5172 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5173 value = wide_int_to_tree (TREE_TYPE (value), value);
5174 }
5175 }
5176
5177 /* Don't let a cast be an lvalue. */
5178 if (value == expr)
5179 value = non_lvalue_loc (loc, value);
5180
5181 /* Don't allow the results of casting to floating-point or complex
5182 types be confused with actual constants, or casts involving
5183 integer and pointer types other than direct integer-to-integer
5184 and integer-to-pointer be confused with integer constant
5185 expressions and null pointer constants. */
5186 if (TREE_CODE (value) == REAL_CST
5187 || TREE_CODE (value) == COMPLEX_CST
5188 || (TREE_CODE (value) == INTEGER_CST
5189 && !((TREE_CODE (expr) == INTEGER_CST
5190 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5191 || TREE_CODE (expr) == REAL_CST
5192 || TREE_CODE (expr) == COMPLEX_CST)))
5193 value = build1 (NOP_EXPR, type, value);
5194
5195 if (CAN_HAVE_LOCATION_P (value))
5196 SET_EXPR_LOCATION (value, loc);
5197 return value;
5198 }
5199
5200 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5201 location of the open paren of the cast, or the position of the cast
5202 expr. */
5203 tree
5204 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5205 {
5206 tree type;
5207 tree type_expr = NULL_TREE;
5208 bool type_expr_const = true;
5209 tree ret;
5210 int saved_wsp = warn_strict_prototypes;
5211
5212 /* This avoids warnings about unprototyped casts on
5213 integers. E.g. "#define SIG_DFL (void(*)())0". */
5214 if (TREE_CODE (expr) == INTEGER_CST)
5215 warn_strict_prototypes = 0;
5216 type = groktypename (type_name, &type_expr, &type_expr_const);
5217 warn_strict_prototypes = saved_wsp;
5218
5219 ret = build_c_cast (loc, type, expr);
5220 if (type_expr)
5221 {
5222 bool inner_expr_const = true;
5223 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5224 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5225 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5226 && inner_expr_const);
5227 SET_EXPR_LOCATION (ret, loc);
5228 }
5229
5230 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5231 SET_EXPR_LOCATION (ret, loc);
5232
5233 /* C++ does not permits types to be defined in a cast, but it
5234 allows references to incomplete types. */
5235 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5236 warning_at (loc, OPT_Wc___compat,
5237 "defining a type in a cast is invalid in C++");
5238
5239 return ret;
5240 }
5241 \f
5242 /* Build an assignment expression of lvalue LHS from value RHS.
5243 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5244 may differ from TREE_TYPE (LHS) for an enum bitfield.
5245 MODIFYCODE is the code for a binary operator that we use
5246 to combine the old value of LHS with RHS to get the new value.
5247 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5248 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5249 which may differ from TREE_TYPE (RHS) for an enum value.
5250
5251 LOCATION is the location of the MODIFYCODE operator.
5252 RHS_LOC is the location of the RHS. */
5253
5254 tree
5255 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5256 enum tree_code modifycode,
5257 location_t rhs_loc, tree rhs, tree rhs_origtype)
5258 {
5259 tree result;
5260 tree newrhs;
5261 tree rhseval = NULL_TREE;
5262 tree rhs_semantic_type = NULL_TREE;
5263 tree lhstype = TREE_TYPE (lhs);
5264 tree olhstype = lhstype;
5265 bool npc;
5266 bool is_atomic_op;
5267
5268 /* Types that aren't fully specified cannot be used in assignments. */
5269 lhs = require_complete_type (lhs);
5270
5271 /* Avoid duplicate error messages from operands that had errors. */
5272 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5273 return error_mark_node;
5274
5275 /* Ensure an error for assigning a non-lvalue array to an array in
5276 C90. */
5277 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5278 {
5279 error_at (location, "assignment to expression with array type");
5280 return error_mark_node;
5281 }
5282
5283 /* For ObjC properties, defer this check. */
5284 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5285 return error_mark_node;
5286
5287 is_atomic_op = really_atomic_lvalue (lhs);
5288
5289 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5290 {
5291 rhs_semantic_type = TREE_TYPE (rhs);
5292 rhs = TREE_OPERAND (rhs, 0);
5293 }
5294
5295 newrhs = rhs;
5296
5297 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5298 {
5299 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5300 lhs_origtype, modifycode, rhs_loc, rhs,
5301 rhs_origtype);
5302 if (inner == error_mark_node)
5303 return error_mark_node;
5304 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5305 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5306 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5307 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5308 protected_set_expr_location (result, location);
5309 return result;
5310 }
5311
5312 /* If a binary op has been requested, combine the old LHS value with the RHS
5313 producing the value we should actually store into the LHS. */
5314
5315 if (modifycode != NOP_EXPR)
5316 {
5317 lhs = c_fully_fold (lhs, false, NULL);
5318 lhs = stabilize_reference (lhs);
5319
5320 /* Construct the RHS for any non-atomic compound assignemnt. */
5321 if (!is_atomic_op)
5322 {
5323 /* If in LHS op= RHS the RHS has side-effects, ensure they
5324 are preevaluated before the rest of the assignment expression's
5325 side-effects, because RHS could contain e.g. function calls
5326 that modify LHS. */
5327 if (TREE_SIDE_EFFECTS (rhs))
5328 {
5329 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5330 rhseval = newrhs;
5331 }
5332 newrhs = build_binary_op (location,
5333 modifycode, lhs, newrhs, 1);
5334
5335 /* The original type of the right hand side is no longer
5336 meaningful. */
5337 rhs_origtype = NULL_TREE;
5338 }
5339 }
5340
5341 if (c_dialect_objc ())
5342 {
5343 /* Check if we are modifying an Objective-C property reference;
5344 if so, we need to generate setter calls. */
5345 result = objc_maybe_build_modify_expr (lhs, newrhs);
5346 if (result)
5347 goto return_result;
5348
5349 /* Else, do the check that we postponed for Objective-C. */
5350 if (!lvalue_or_else (location, lhs, lv_assign))
5351 return error_mark_node;
5352 }
5353
5354 /* Give an error for storing in something that is 'const'. */
5355
5356 if (TYPE_READONLY (lhstype)
5357 || ((TREE_CODE (lhstype) == RECORD_TYPE
5358 || TREE_CODE (lhstype) == UNION_TYPE)
5359 && C_TYPE_FIELDS_READONLY (lhstype)))
5360 {
5361 readonly_error (location, lhs, lv_assign);
5362 return error_mark_node;
5363 }
5364 else if (TREE_READONLY (lhs))
5365 readonly_warning (lhs, lv_assign);
5366
5367 /* If storing into a structure or union member,
5368 it has probably been given type `int'.
5369 Compute the type that would go with
5370 the actual amount of storage the member occupies. */
5371
5372 if (TREE_CODE (lhs) == COMPONENT_REF
5373 && (TREE_CODE (lhstype) == INTEGER_TYPE
5374 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5375 || TREE_CODE (lhstype) == REAL_TYPE
5376 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5377 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5378
5379 /* If storing in a field that is in actuality a short or narrower than one,
5380 we must store in the field in its actual type. */
5381
5382 if (lhstype != TREE_TYPE (lhs))
5383 {
5384 lhs = copy_node (lhs);
5385 TREE_TYPE (lhs) = lhstype;
5386 }
5387
5388 /* Issue -Wc++-compat warnings about an assignment to an enum type
5389 when LHS does not have its original type. This happens for,
5390 e.g., an enum bitfield in a struct. */
5391 if (warn_cxx_compat
5392 && lhs_origtype != NULL_TREE
5393 && lhs_origtype != lhstype
5394 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5395 {
5396 tree checktype = (rhs_origtype != NULL_TREE
5397 ? rhs_origtype
5398 : TREE_TYPE (rhs));
5399 if (checktype != error_mark_node
5400 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5401 || (is_atomic_op && modifycode != NOP_EXPR)))
5402 warning_at (location, OPT_Wc___compat,
5403 "enum conversion in assignment is invalid in C++");
5404 }
5405
5406 /* If the lhs is atomic, remove that qualifier. */
5407 if (is_atomic_op)
5408 {
5409 lhstype = build_qualified_type (lhstype,
5410 (TYPE_QUALS (lhstype)
5411 & ~TYPE_QUAL_ATOMIC));
5412 olhstype = build_qualified_type (olhstype,
5413 (TYPE_QUALS (lhstype)
5414 & ~TYPE_QUAL_ATOMIC));
5415 }
5416
5417 /* Convert new value to destination type. Fold it first, then
5418 restore any excess precision information, for the sake of
5419 conversion warnings. */
5420
5421 if (!(is_atomic_op && modifycode != NOP_EXPR))
5422 {
5423 npc = null_pointer_constant_p (newrhs);
5424 newrhs = c_fully_fold (newrhs, false, NULL);
5425 if (rhs_semantic_type)
5426 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5427 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5428 rhs_origtype, ic_assign, npc,
5429 NULL_TREE, NULL_TREE, 0);
5430 if (TREE_CODE (newrhs) == ERROR_MARK)
5431 return error_mark_node;
5432 }
5433
5434 /* Emit ObjC write barrier, if necessary. */
5435 if (c_dialect_objc () && flag_objc_gc)
5436 {
5437 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5438 if (result)
5439 {
5440 protected_set_expr_location (result, location);
5441 goto return_result;
5442 }
5443 }
5444
5445 /* Scan operands. */
5446
5447 if (is_atomic_op)
5448 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5449 else
5450 {
5451 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5452 TREE_SIDE_EFFECTS (result) = 1;
5453 protected_set_expr_location (result, location);
5454 }
5455
5456 /* If we got the LHS in a different type for storing in,
5457 convert the result back to the nominal type of LHS
5458 so that the value we return always has the same type
5459 as the LHS argument. */
5460
5461 if (olhstype == TREE_TYPE (result))
5462 goto return_result;
5463
5464 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5465 rhs_origtype, ic_assign, false, NULL_TREE,
5466 NULL_TREE, 0);
5467 protected_set_expr_location (result, location);
5468
5469 return_result:
5470 if (rhseval)
5471 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5472 return result;
5473 }
5474 \f
5475 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5476 This is used to implement -fplan9-extensions. */
5477
5478 static bool
5479 find_anonymous_field_with_type (tree struct_type, tree type)
5480 {
5481 tree field;
5482 bool found;
5483
5484 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5485 || TREE_CODE (struct_type) == UNION_TYPE);
5486 found = false;
5487 for (field = TYPE_FIELDS (struct_type);
5488 field != NULL_TREE;
5489 field = TREE_CHAIN (field))
5490 {
5491 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5492 ? c_build_qualified_type (TREE_TYPE (field),
5493 TYPE_QUAL_ATOMIC)
5494 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5495 if (DECL_NAME (field) == NULL
5496 && comptypes (type, fieldtype))
5497 {
5498 if (found)
5499 return false;
5500 found = true;
5501 }
5502 else if (DECL_NAME (field) == NULL
5503 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5504 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5505 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5506 {
5507 if (found)
5508 return false;
5509 found = true;
5510 }
5511 }
5512 return found;
5513 }
5514
5515 /* RHS is an expression whose type is pointer to struct. If there is
5516 an anonymous field in RHS with type TYPE, then return a pointer to
5517 that field in RHS. This is used with -fplan9-extensions. This
5518 returns NULL if no conversion could be found. */
5519
5520 static tree
5521 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5522 {
5523 tree rhs_struct_type, lhs_main_type;
5524 tree field, found_field;
5525 bool found_sub_field;
5526 tree ret;
5527
5528 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5529 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5530 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5531 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5532
5533 gcc_assert (POINTER_TYPE_P (type));
5534 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5535 ? c_build_qualified_type (TREE_TYPE (type),
5536 TYPE_QUAL_ATOMIC)
5537 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5538
5539 found_field = NULL_TREE;
5540 found_sub_field = false;
5541 for (field = TYPE_FIELDS (rhs_struct_type);
5542 field != NULL_TREE;
5543 field = TREE_CHAIN (field))
5544 {
5545 if (DECL_NAME (field) != NULL_TREE
5546 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5547 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5548 continue;
5549 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5550 ? c_build_qualified_type (TREE_TYPE (field),
5551 TYPE_QUAL_ATOMIC)
5552 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5553 if (comptypes (lhs_main_type, fieldtype))
5554 {
5555 if (found_field != NULL_TREE)
5556 return NULL_TREE;
5557 found_field = field;
5558 }
5559 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5560 lhs_main_type))
5561 {
5562 if (found_field != NULL_TREE)
5563 return NULL_TREE;
5564 found_field = field;
5565 found_sub_field = true;
5566 }
5567 }
5568
5569 if (found_field == NULL_TREE)
5570 return NULL_TREE;
5571
5572 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5573 build_fold_indirect_ref (rhs), found_field,
5574 NULL_TREE);
5575 ret = build_fold_addr_expr_loc (location, ret);
5576
5577 if (found_sub_field)
5578 {
5579 ret = convert_to_anonymous_field (location, type, ret);
5580 gcc_assert (ret != NULL_TREE);
5581 }
5582
5583 return ret;
5584 }
5585
5586 /* Issue an error message for a bad initializer component.
5587 GMSGID identifies the message.
5588 The component name is taken from the spelling stack. */
5589
5590 static void
5591 error_init (location_t loc, const char *gmsgid)
5592 {
5593 char *ofwhat;
5594
5595 /* The gmsgid may be a format string with %< and %>. */
5596 error_at (loc, gmsgid);
5597 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5598 if (*ofwhat)
5599 inform (loc, "(near initialization for %qs)", ofwhat);
5600 }
5601
5602 /* Issue a pedantic warning for a bad initializer component. OPT is
5603 the option OPT_* (from options.h) controlling this warning or 0 if
5604 it is unconditionally given. GMSGID identifies the message. The
5605 component name is taken from the spelling stack. */
5606
5607 static void
5608 pedwarn_init (location_t location, int opt, const char *gmsgid)
5609 {
5610 char *ofwhat;
5611 bool warned;
5612
5613 /* The gmsgid may be a format string with %< and %>. */
5614 warned = pedwarn (location, opt, gmsgid);
5615 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5616 if (*ofwhat && warned)
5617 inform (location, "(near initialization for %qs)", ofwhat);
5618 }
5619
5620 /* Issue a warning for a bad initializer component.
5621
5622 OPT is the OPT_W* value corresponding to the warning option that
5623 controls this warning. GMSGID identifies the message. The
5624 component name is taken from the spelling stack. */
5625
5626 static void
5627 warning_init (location_t loc, int opt, const char *gmsgid)
5628 {
5629 char *ofwhat;
5630 bool warned;
5631
5632 /* The gmsgid may be a format string with %< and %>. */
5633 warned = warning_at (loc, opt, gmsgid);
5634 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5635 if (*ofwhat && warned)
5636 inform (loc, "(near initialization for %qs)", ofwhat);
5637 }
5638 \f
5639 /* If TYPE is an array type and EXPR is a parenthesized string
5640 constant, warn if pedantic that EXPR is being used to initialize an
5641 object of type TYPE. */
5642
5643 void
5644 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5645 {
5646 if (pedantic
5647 && TREE_CODE (type) == ARRAY_TYPE
5648 && TREE_CODE (expr.value) == STRING_CST
5649 && expr.original_code != STRING_CST)
5650 pedwarn_init (loc, OPT_Wpedantic,
5651 "array initialized from parenthesized string constant");
5652 }
5653
5654 /* Convert value RHS to type TYPE as preparation for an assignment to
5655 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5656 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5657 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5658 constant before any folding.
5659 The real work of conversion is done by `convert'.
5660 The purpose of this function is to generate error messages
5661 for assignments that are not allowed in C.
5662 ERRTYPE says whether it is argument passing, assignment,
5663 initialization or return.
5664
5665 LOCATION is the location of the assignment, EXPR_LOC is the location of
5666 the RHS or, for a function, location of an argument.
5667 FUNCTION is a tree for the function being called.
5668 PARMNUM is the number of the argument, for printing in error messages. */
5669
5670 static tree
5671 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5672 tree rhs, tree origtype, enum impl_conv errtype,
5673 bool null_pointer_constant, tree fundecl,
5674 tree function, int parmnum)
5675 {
5676 enum tree_code codel = TREE_CODE (type);
5677 tree orig_rhs = rhs;
5678 tree rhstype;
5679 enum tree_code coder;
5680 tree rname = NULL_TREE;
5681 bool objc_ok = false;
5682
5683 if (errtype == ic_argpass)
5684 {
5685 tree selector;
5686 /* Change pointer to function to the function itself for
5687 diagnostics. */
5688 if (TREE_CODE (function) == ADDR_EXPR
5689 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5690 function = TREE_OPERAND (function, 0);
5691
5692 /* Handle an ObjC selector specially for diagnostics. */
5693 selector = objc_message_selector ();
5694 rname = function;
5695 if (selector && parmnum > 2)
5696 {
5697 rname = selector;
5698 parmnum -= 2;
5699 }
5700 }
5701
5702 /* This macro is used to emit diagnostics to ensure that all format
5703 strings are complete sentences, visible to gettext and checked at
5704 compile time. */
5705 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5706 do { \
5707 switch (errtype) \
5708 { \
5709 case ic_argpass: \
5710 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5711 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5712 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5713 "expected %qT but argument is of type %qT", \
5714 type, rhstype); \
5715 break; \
5716 case ic_assign: \
5717 pedwarn (LOCATION, OPT, AS); \
5718 break; \
5719 case ic_init: \
5720 pedwarn_init (LOCATION, OPT, IN); \
5721 break; \
5722 case ic_return: \
5723 pedwarn (LOCATION, OPT, RE); \
5724 break; \
5725 default: \
5726 gcc_unreachable (); \
5727 } \
5728 } while (0)
5729
5730 /* This macro is used to emit diagnostics to ensure that all format
5731 strings are complete sentences, visible to gettext and checked at
5732 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
5733 extra parameter to enumerate qualifiers. */
5734 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5735 do { \
5736 switch (errtype) \
5737 { \
5738 case ic_argpass: \
5739 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5740 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5741 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5742 "expected %qT but argument is of type %qT", \
5743 type, rhstype); \
5744 break; \
5745 case ic_assign: \
5746 pedwarn (LOCATION, OPT, AS, QUALS); \
5747 break; \
5748 case ic_init: \
5749 pedwarn (LOCATION, OPT, IN, QUALS); \
5750 break; \
5751 case ic_return: \
5752 pedwarn (LOCATION, OPT, RE, QUALS); \
5753 break; \
5754 default: \
5755 gcc_unreachable (); \
5756 } \
5757 } while (0)
5758
5759 /* This macro is used to emit diagnostics to ensure that all format
5760 strings are complete sentences, visible to gettext and checked at
5761 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5762 warning_at instead of pedwarn. */
5763 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5764 do { \
5765 switch (errtype) \
5766 { \
5767 case ic_argpass: \
5768 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5769 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5770 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5771 "expected %qT but argument is of type %qT", \
5772 type, rhstype); \
5773 break; \
5774 case ic_assign: \
5775 warning_at (LOCATION, OPT, AS, QUALS); \
5776 break; \
5777 case ic_init: \
5778 warning_at (LOCATION, OPT, IN, QUALS); \
5779 break; \
5780 case ic_return: \
5781 warning_at (LOCATION, OPT, RE, QUALS); \
5782 break; \
5783 default: \
5784 gcc_unreachable (); \
5785 } \
5786 } while (0)
5787
5788 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5789 rhs = TREE_OPERAND (rhs, 0);
5790
5791 rhstype = TREE_TYPE (rhs);
5792 coder = TREE_CODE (rhstype);
5793
5794 if (coder == ERROR_MARK)
5795 return error_mark_node;
5796
5797 if (c_dialect_objc ())
5798 {
5799 int parmno;
5800
5801 switch (errtype)
5802 {
5803 case ic_return:
5804 parmno = 0;
5805 break;
5806
5807 case ic_assign:
5808 parmno = -1;
5809 break;
5810
5811 case ic_init:
5812 parmno = -2;
5813 break;
5814
5815 default:
5816 parmno = parmnum;
5817 break;
5818 }
5819
5820 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5821 }
5822
5823 if (warn_cxx_compat)
5824 {
5825 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5826 if (checktype != error_mark_node
5827 && TREE_CODE (type) == ENUMERAL_TYPE
5828 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5829 {
5830 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5831 G_("enum conversion when passing argument "
5832 "%d of %qE is invalid in C++"),
5833 G_("enum conversion in assignment is "
5834 "invalid in C++"),
5835 G_("enum conversion in initialization is "
5836 "invalid in C++"),
5837 G_("enum conversion in return is "
5838 "invalid in C++"));
5839 }
5840 }
5841
5842 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5843 return rhs;
5844
5845 if (coder == VOID_TYPE)
5846 {
5847 /* Except for passing an argument to an unprototyped function,
5848 this is a constraint violation. When passing an argument to
5849 an unprototyped function, it is compile-time undefined;
5850 making it a constraint in that case was rejected in
5851 DR#252. */
5852 error_at (location, "void value not ignored as it ought to be");
5853 return error_mark_node;
5854 }
5855 rhs = require_complete_type (rhs);
5856 if (rhs == error_mark_node)
5857 return error_mark_node;
5858 /* A non-reference type can convert to a reference. This handles
5859 va_start, va_copy and possibly port built-ins. */
5860 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5861 {
5862 if (!lvalue_p (rhs))
5863 {
5864 error_at (location, "cannot pass rvalue to reference parameter");
5865 return error_mark_node;
5866 }
5867 if (!c_mark_addressable (rhs))
5868 return error_mark_node;
5869 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5870 SET_EXPR_LOCATION (rhs, location);
5871
5872 rhs = convert_for_assignment (location, expr_loc,
5873 build_pointer_type (TREE_TYPE (type)),
5874 rhs, origtype, errtype,
5875 null_pointer_constant, fundecl, function,
5876 parmnum);
5877 if (rhs == error_mark_node)
5878 return error_mark_node;
5879
5880 rhs = build1 (NOP_EXPR, type, rhs);
5881 SET_EXPR_LOCATION (rhs, location);
5882 return rhs;
5883 }
5884 /* Some types can interconvert without explicit casts. */
5885 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5886 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5887 return convert (type, rhs);
5888 /* Arithmetic types all interconvert, and enum is treated like int. */
5889 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5890 || codel == FIXED_POINT_TYPE
5891 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5892 || codel == BOOLEAN_TYPE)
5893 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5894 || coder == FIXED_POINT_TYPE
5895 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5896 || coder == BOOLEAN_TYPE))
5897 {
5898 tree ret;
5899 bool save = in_late_binary_op;
5900 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
5901 || (coder == REAL_TYPE
5902 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
5903 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
5904 in_late_binary_op = true;
5905 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5906 ? expr_loc : location, type, orig_rhs);
5907 in_late_binary_op = save;
5908 return ret;
5909 }
5910
5911 /* Aggregates in different TUs might need conversion. */
5912 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5913 && codel == coder
5914 && comptypes (type, rhstype))
5915 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5916 ? expr_loc : location, type, rhs);
5917
5918 /* Conversion to a transparent union or record from its member types.
5919 This applies only to function arguments. */
5920 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5921 && TYPE_TRANSPARENT_AGGR (type))
5922 && errtype == ic_argpass)
5923 {
5924 tree memb, marginal_memb = NULL_TREE;
5925
5926 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5927 {
5928 tree memb_type = TREE_TYPE (memb);
5929
5930 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5931 TYPE_MAIN_VARIANT (rhstype)))
5932 break;
5933
5934 if (TREE_CODE (memb_type) != POINTER_TYPE)
5935 continue;
5936
5937 if (coder == POINTER_TYPE)
5938 {
5939 tree ttl = TREE_TYPE (memb_type);
5940 tree ttr = TREE_TYPE (rhstype);
5941
5942 /* Any non-function converts to a [const][volatile] void *
5943 and vice versa; otherwise, targets must be the same.
5944 Meanwhile, the lhs target must have all the qualifiers of
5945 the rhs. */
5946 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5947 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
5948 || comp_target_types (location, memb_type, rhstype))
5949 {
5950 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5951 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
5952 /* If this type won't generate any warnings, use it. */
5953 if (lquals == rquals
5954 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5955 && TREE_CODE (ttl) == FUNCTION_TYPE)
5956 ? ((lquals | rquals) == rquals)
5957 : ((lquals | rquals) == lquals)))
5958 break;
5959
5960 /* Keep looking for a better type, but remember this one. */
5961 if (!marginal_memb)
5962 marginal_memb = memb;
5963 }
5964 }
5965
5966 /* Can convert integer zero to any pointer type. */
5967 if (null_pointer_constant)
5968 {
5969 rhs = null_pointer_node;
5970 break;
5971 }
5972 }
5973
5974 if (memb || marginal_memb)
5975 {
5976 if (!memb)
5977 {
5978 /* We have only a marginally acceptable member type;
5979 it needs a warning. */
5980 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5981 tree ttr = TREE_TYPE (rhstype);
5982
5983 /* Const and volatile mean something different for function
5984 types, so the usual warnings are not appropriate. */
5985 if (TREE_CODE (ttr) == FUNCTION_TYPE
5986 && TREE_CODE (ttl) == FUNCTION_TYPE)
5987 {
5988 /* Because const and volatile on functions are
5989 restrictions that say the function will not do
5990 certain things, it is okay to use a const or volatile
5991 function where an ordinary one is wanted, but not
5992 vice-versa. */
5993 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5994 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5995 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
5996 OPT_Wdiscarded_qualifiers,
5997 G_("passing argument %d of %qE "
5998 "makes %q#v qualified function "
5999 "pointer from unqualified"),
6000 G_("assignment makes %q#v qualified "
6001 "function pointer from "
6002 "unqualified"),
6003 G_("initialization makes %q#v qualified "
6004 "function pointer from "
6005 "unqualified"),
6006 G_("return makes %q#v qualified function "
6007 "pointer from unqualified"),
6008 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6009 }
6010 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6011 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6012 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6013 OPT_Wdiscarded_qualifiers,
6014 G_("passing argument %d of %qE discards "
6015 "%qv qualifier from pointer target type"),
6016 G_("assignment discards %qv qualifier "
6017 "from pointer target type"),
6018 G_("initialization discards %qv qualifier "
6019 "from pointer target type"),
6020 G_("return discards %qv qualifier from "
6021 "pointer target type"),
6022 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6023
6024 memb = marginal_memb;
6025 }
6026
6027 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6028 pedwarn (location, OPT_Wpedantic,
6029 "ISO C prohibits argument conversion to union type");
6030
6031 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6032 return build_constructor_single (type, memb, rhs);
6033 }
6034 }
6035
6036 /* Conversions among pointers */
6037 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6038 && (coder == codel))
6039 {
6040 tree ttl = TREE_TYPE (type);
6041 tree ttr = TREE_TYPE (rhstype);
6042 tree mvl = ttl;
6043 tree mvr = ttr;
6044 bool is_opaque_pointer;
6045 int target_cmp = 0; /* Cache comp_target_types () result. */
6046 addr_space_t asl;
6047 addr_space_t asr;
6048
6049 if (TREE_CODE (mvl) != ARRAY_TYPE)
6050 mvl = (TYPE_ATOMIC (mvl)
6051 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6052 TYPE_QUAL_ATOMIC)
6053 : TYPE_MAIN_VARIANT (mvl));
6054 if (TREE_CODE (mvr) != ARRAY_TYPE)
6055 mvr = (TYPE_ATOMIC (mvr)
6056 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6057 TYPE_QUAL_ATOMIC)
6058 : TYPE_MAIN_VARIANT (mvr));
6059 /* Opaque pointers are treated like void pointers. */
6060 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6061
6062 /* The Plan 9 compiler permits a pointer to a struct to be
6063 automatically converted into a pointer to an anonymous field
6064 within the struct. */
6065 if (flag_plan9_extensions
6066 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6067 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6068 && mvl != mvr)
6069 {
6070 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6071 if (new_rhs != NULL_TREE)
6072 {
6073 rhs = new_rhs;
6074 rhstype = TREE_TYPE (rhs);
6075 coder = TREE_CODE (rhstype);
6076 ttr = TREE_TYPE (rhstype);
6077 mvr = TYPE_MAIN_VARIANT (ttr);
6078 }
6079 }
6080
6081 /* C++ does not allow the implicit conversion void* -> T*. However,
6082 for the purpose of reducing the number of false positives, we
6083 tolerate the special case of
6084
6085 int *p = NULL;
6086
6087 where NULL is typically defined in C to be '(void *) 0'. */
6088 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6089 warning_at (errtype == ic_argpass ? expr_loc : location,
6090 OPT_Wc___compat,
6091 "request for implicit conversion "
6092 "from %qT to %qT not permitted in C++", rhstype, type);
6093
6094 /* See if the pointers point to incompatible address spaces. */
6095 asl = TYPE_ADDR_SPACE (ttl);
6096 asr = TYPE_ADDR_SPACE (ttr);
6097 if (!null_pointer_constant_p (rhs)
6098 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6099 {
6100 switch (errtype)
6101 {
6102 case ic_argpass:
6103 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6104 "non-enclosed address space", parmnum, rname);
6105 break;
6106 case ic_assign:
6107 error_at (location, "assignment from pointer to "
6108 "non-enclosed address space");
6109 break;
6110 case ic_init:
6111 error_at (location, "initialization from pointer to "
6112 "non-enclosed address space");
6113 break;
6114 case ic_return:
6115 error_at (location, "return from pointer to "
6116 "non-enclosed address space");
6117 break;
6118 default:
6119 gcc_unreachable ();
6120 }
6121 return error_mark_node;
6122 }
6123
6124 /* Check if the right-hand side has a format attribute but the
6125 left-hand side doesn't. */
6126 if (warn_suggest_attribute_format
6127 && check_missing_format_attribute (type, rhstype))
6128 {
6129 switch (errtype)
6130 {
6131 case ic_argpass:
6132 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6133 "argument %d of %qE might be "
6134 "a candidate for a format attribute",
6135 parmnum, rname);
6136 break;
6137 case ic_assign:
6138 warning_at (location, OPT_Wsuggest_attribute_format,
6139 "assignment left-hand side might be "
6140 "a candidate for a format attribute");
6141 break;
6142 case ic_init:
6143 warning_at (location, OPT_Wsuggest_attribute_format,
6144 "initialization left-hand side might be "
6145 "a candidate for a format attribute");
6146 break;
6147 case ic_return:
6148 warning_at (location, OPT_Wsuggest_attribute_format,
6149 "return type might be "
6150 "a candidate for a format attribute");
6151 break;
6152 default:
6153 gcc_unreachable ();
6154 }
6155 }
6156
6157 /* Any non-function converts to a [const][volatile] void *
6158 and vice versa; otherwise, targets must be the same.
6159 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6160 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6161 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6162 || (target_cmp = comp_target_types (location, type, rhstype))
6163 || is_opaque_pointer
6164 || ((c_common_unsigned_type (mvl)
6165 == c_common_unsigned_type (mvr))
6166 && (c_common_signed_type (mvl)
6167 == c_common_signed_type (mvr))
6168 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6169 {
6170 /* Warn about loss of qualifers from pointers to arrays with
6171 qualifiers on the element type. */
6172 if (TREE_CODE (ttr) == ARRAY_TYPE)
6173 {
6174 ttr = strip_array_types (ttr);
6175 ttl = strip_array_types (ttl);
6176
6177 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6178 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6179 WARNING_FOR_QUALIFIERS (location, expr_loc,
6180 OPT_Wdiscarded_array_qualifiers,
6181 G_("passing argument %d of %qE discards "
6182 "%qv qualifier from pointer target type"),
6183 G_("assignment discards %qv qualifier "
6184 "from pointer target type"),
6185 G_("initialization discards %qv qualifier "
6186 "from pointer target type"),
6187 G_("return discards %qv qualifier from "
6188 "pointer target type"),
6189 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6190 }
6191 else if (pedantic
6192 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6193 ||
6194 (VOID_TYPE_P (ttr)
6195 && !null_pointer_constant
6196 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6197 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6198 G_("ISO C forbids passing argument %d of "
6199 "%qE between function pointer "
6200 "and %<void *%>"),
6201 G_("ISO C forbids assignment between "
6202 "function pointer and %<void *%>"),
6203 G_("ISO C forbids initialization between "
6204 "function pointer and %<void *%>"),
6205 G_("ISO C forbids return between function "
6206 "pointer and %<void *%>"));
6207 /* Const and volatile mean something different for function types,
6208 so the usual warnings are not appropriate. */
6209 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6210 && TREE_CODE (ttl) != FUNCTION_TYPE)
6211 {
6212 /* Don't warn about loss of qualifier for conversions from
6213 qualified void* to pointers to arrays with corresponding
6214 qualifier on the element type. */
6215 if (!pedantic)
6216 ttl = strip_array_types (ttl);
6217
6218 /* Assignments between atomic and non-atomic objects are OK. */
6219 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6220 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6221 {
6222 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6223 OPT_Wdiscarded_qualifiers,
6224 G_("passing argument %d of %qE discards "
6225 "%qv qualifier from pointer target type"),
6226 G_("assignment discards %qv qualifier "
6227 "from pointer target type"),
6228 G_("initialization discards %qv qualifier "
6229 "from pointer target type"),
6230 G_("return discards %qv qualifier from "
6231 "pointer target type"),
6232 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6233 }
6234 /* If this is not a case of ignoring a mismatch in signedness,
6235 no warning. */
6236 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6237 || target_cmp)
6238 ;
6239 /* If there is a mismatch, do warn. */
6240 else if (warn_pointer_sign)
6241 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6242 G_("pointer targets in passing argument "
6243 "%d of %qE differ in signedness"),
6244 G_("pointer targets in assignment "
6245 "differ in signedness"),
6246 G_("pointer targets in initialization "
6247 "differ in signedness"),
6248 G_("pointer targets in return differ "
6249 "in signedness"));
6250 }
6251 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6252 && TREE_CODE (ttr) == FUNCTION_TYPE)
6253 {
6254 /* Because const and volatile on functions are restrictions
6255 that say the function will not do certain things,
6256 it is okay to use a const or volatile function
6257 where an ordinary one is wanted, but not vice-versa. */
6258 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6259 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6260 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6261 OPT_Wdiscarded_qualifiers,
6262 G_("passing argument %d of %qE makes "
6263 "%q#v qualified function pointer "
6264 "from unqualified"),
6265 G_("assignment makes %q#v qualified function "
6266 "pointer from unqualified"),
6267 G_("initialization makes %q#v qualified "
6268 "function pointer from unqualified"),
6269 G_("return makes %q#v qualified function "
6270 "pointer from unqualified"),
6271 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6272 }
6273 }
6274 else
6275 /* Avoid warning about the volatile ObjC EH puts on decls. */
6276 if (!objc_ok)
6277 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6278 OPT_Wincompatible_pointer_types,
6279 G_("passing argument %d of %qE from "
6280 "incompatible pointer type"),
6281 G_("assignment from incompatible pointer type"),
6282 G_("initialization from incompatible "
6283 "pointer type"),
6284 G_("return from incompatible pointer type"));
6285
6286 return convert (type, rhs);
6287 }
6288 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6289 {
6290 /* ??? This should not be an error when inlining calls to
6291 unprototyped functions. */
6292 error_at (location, "invalid use of non-lvalue array");
6293 return error_mark_node;
6294 }
6295 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6296 {
6297 /* An explicit constant 0 can convert to a pointer,
6298 or one that results from arithmetic, even including
6299 a cast to integer type. */
6300 if (!null_pointer_constant)
6301 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6302 OPT_Wint_conversion,
6303 G_("passing argument %d of %qE makes "
6304 "pointer from integer without a cast"),
6305 G_("assignment makes pointer from integer "
6306 "without a cast"),
6307 G_("initialization makes pointer from "
6308 "integer without a cast"),
6309 G_("return makes pointer from integer "
6310 "without a cast"));
6311
6312 return convert (type, rhs);
6313 }
6314 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6315 {
6316 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6317 OPT_Wint_conversion,
6318 G_("passing argument %d of %qE makes integer "
6319 "from pointer without a cast"),
6320 G_("assignment makes integer from pointer "
6321 "without a cast"),
6322 G_("initialization makes integer from pointer "
6323 "without a cast"),
6324 G_("return makes integer from pointer "
6325 "without a cast"));
6326 return convert (type, rhs);
6327 }
6328 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6329 {
6330 tree ret;
6331 bool save = in_late_binary_op;
6332 in_late_binary_op = true;
6333 ret = convert (type, rhs);
6334 in_late_binary_op = save;
6335 return ret;
6336 }
6337
6338 switch (errtype)
6339 {
6340 case ic_argpass:
6341 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6342 rname);
6343 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6344 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6345 "expected %qT but argument is of type %qT", type, rhstype);
6346 break;
6347 case ic_assign:
6348 error_at (location, "incompatible types when assigning to type %qT from "
6349 "type %qT", type, rhstype);
6350 break;
6351 case ic_init:
6352 error_at (location,
6353 "incompatible types when initializing type %qT using type %qT",
6354 type, rhstype);
6355 break;
6356 case ic_return:
6357 error_at (location,
6358 "incompatible types when returning type %qT but %qT was "
6359 "expected", rhstype, type);
6360 break;
6361 default:
6362 gcc_unreachable ();
6363 }
6364
6365 return error_mark_node;
6366 }
6367 \f
6368 /* If VALUE is a compound expr all of whose expressions are constant, then
6369 return its value. Otherwise, return error_mark_node.
6370
6371 This is for handling COMPOUND_EXPRs as initializer elements
6372 which is allowed with a warning when -pedantic is specified. */
6373
6374 static tree
6375 valid_compound_expr_initializer (tree value, tree endtype)
6376 {
6377 if (TREE_CODE (value) == COMPOUND_EXPR)
6378 {
6379 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6380 == error_mark_node)
6381 return error_mark_node;
6382 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6383 endtype);
6384 }
6385 else if (!initializer_constant_valid_p (value, endtype))
6386 return error_mark_node;
6387 else
6388 return value;
6389 }
6390 \f
6391 /* Perform appropriate conversions on the initial value of a variable,
6392 store it in the declaration DECL,
6393 and print any error messages that are appropriate.
6394 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6395 If the init is invalid, store an ERROR_MARK.
6396
6397 INIT_LOC is the location of the initial value. */
6398
6399 void
6400 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6401 {
6402 tree value, type;
6403 bool npc = false;
6404
6405 /* If variable's type was invalidly declared, just ignore it. */
6406
6407 type = TREE_TYPE (decl);
6408 if (TREE_CODE (type) == ERROR_MARK)
6409 return;
6410
6411 /* Digest the specified initializer into an expression. */
6412
6413 if (init)
6414 npc = null_pointer_constant_p (init);
6415 value = digest_init (init_loc, type, init, origtype, npc,
6416 true, TREE_STATIC (decl));
6417
6418 /* Store the expression if valid; else report error. */
6419
6420 if (!in_system_header_at (input_location)
6421 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6422 warning (OPT_Wtraditional, "traditional C rejects automatic "
6423 "aggregate initialization");
6424
6425 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6426 DECL_INITIAL (decl) = value;
6427
6428 /* ANSI wants warnings about out-of-range constant initializers. */
6429 STRIP_TYPE_NOPS (value);
6430 if (TREE_STATIC (decl))
6431 constant_expression_warning (value);
6432
6433 /* Check if we need to set array size from compound literal size. */
6434 if (TREE_CODE (type) == ARRAY_TYPE
6435 && TYPE_DOMAIN (type) == 0
6436 && value != error_mark_node)
6437 {
6438 tree inside_init = init;
6439
6440 STRIP_TYPE_NOPS (inside_init);
6441 inside_init = fold (inside_init);
6442
6443 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6444 {
6445 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6446
6447 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6448 {
6449 /* For int foo[] = (int [3]){1}; we need to set array size
6450 now since later on array initializer will be just the
6451 brace enclosed list of the compound literal. */
6452 tree etype = strip_array_types (TREE_TYPE (decl));
6453 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6454 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6455 layout_type (type);
6456 layout_decl (cldecl, 0);
6457 TREE_TYPE (decl)
6458 = c_build_qualified_type (type, TYPE_QUALS (etype));
6459 }
6460 }
6461 }
6462 }
6463 \f
6464 /* Methods for storing and printing names for error messages. */
6465
6466 /* Implement a spelling stack that allows components of a name to be pushed
6467 and popped. Each element on the stack is this structure. */
6468
6469 struct spelling
6470 {
6471 int kind;
6472 union
6473 {
6474 unsigned HOST_WIDE_INT i;
6475 const char *s;
6476 } u;
6477 };
6478
6479 #define SPELLING_STRING 1
6480 #define SPELLING_MEMBER 2
6481 #define SPELLING_BOUNDS 3
6482
6483 static struct spelling *spelling; /* Next stack element (unused). */
6484 static struct spelling *spelling_base; /* Spelling stack base. */
6485 static int spelling_size; /* Size of the spelling stack. */
6486
6487 /* Macros to save and restore the spelling stack around push_... functions.
6488 Alternative to SAVE_SPELLING_STACK. */
6489
6490 #define SPELLING_DEPTH() (spelling - spelling_base)
6491 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6492
6493 /* Push an element on the spelling stack with type KIND and assign VALUE
6494 to MEMBER. */
6495
6496 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6497 { \
6498 int depth = SPELLING_DEPTH (); \
6499 \
6500 if (depth >= spelling_size) \
6501 { \
6502 spelling_size += 10; \
6503 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6504 spelling_size); \
6505 RESTORE_SPELLING_DEPTH (depth); \
6506 } \
6507 \
6508 spelling->kind = (KIND); \
6509 spelling->MEMBER = (VALUE); \
6510 spelling++; \
6511 }
6512
6513 /* Push STRING on the stack. Printed literally. */
6514
6515 static void
6516 push_string (const char *string)
6517 {
6518 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6519 }
6520
6521 /* Push a member name on the stack. Printed as '.' STRING. */
6522
6523 static void
6524 push_member_name (tree decl)
6525 {
6526 const char *const string
6527 = (DECL_NAME (decl)
6528 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6529 : _("<anonymous>"));
6530 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6531 }
6532
6533 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6534
6535 static void
6536 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6537 {
6538 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6539 }
6540
6541 /* Compute the maximum size in bytes of the printed spelling. */
6542
6543 static int
6544 spelling_length (void)
6545 {
6546 int size = 0;
6547 struct spelling *p;
6548
6549 for (p = spelling_base; p < spelling; p++)
6550 {
6551 if (p->kind == SPELLING_BOUNDS)
6552 size += 25;
6553 else
6554 size += strlen (p->u.s) + 1;
6555 }
6556
6557 return size;
6558 }
6559
6560 /* Print the spelling to BUFFER and return it. */
6561
6562 static char *
6563 print_spelling (char *buffer)
6564 {
6565 char *d = buffer;
6566 struct spelling *p;
6567
6568 for (p = spelling_base; p < spelling; p++)
6569 if (p->kind == SPELLING_BOUNDS)
6570 {
6571 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6572 d += strlen (d);
6573 }
6574 else
6575 {
6576 const char *s;
6577 if (p->kind == SPELLING_MEMBER)
6578 *d++ = '.';
6579 for (s = p->u.s; (*d = *s++); d++)
6580 ;
6581 }
6582 *d++ = '\0';
6583 return buffer;
6584 }
6585
6586 /* Digest the parser output INIT as an initializer for type TYPE.
6587 Return a C expression of type TYPE to represent the initial value.
6588
6589 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6590
6591 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6592
6593 If INIT is a string constant, STRICT_STRING is true if it is
6594 unparenthesized or we should not warn here for it being parenthesized.
6595 For other types of INIT, STRICT_STRING is not used.
6596
6597 INIT_LOC is the location of the INIT.
6598
6599 REQUIRE_CONSTANT requests an error if non-constant initializers or
6600 elements are seen. */
6601
6602 static tree
6603 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6604 bool null_pointer_constant, bool strict_string,
6605 int require_constant)
6606 {
6607 enum tree_code code = TREE_CODE (type);
6608 tree inside_init = init;
6609 tree semantic_type = NULL_TREE;
6610 bool maybe_const = true;
6611
6612 if (type == error_mark_node
6613 || !init
6614 || error_operand_p (init))
6615 return error_mark_node;
6616
6617 STRIP_TYPE_NOPS (inside_init);
6618
6619 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6620 {
6621 semantic_type = TREE_TYPE (inside_init);
6622 inside_init = TREE_OPERAND (inside_init, 0);
6623 }
6624 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6625 inside_init = decl_constant_value_for_optimization (inside_init);
6626
6627 /* Initialization of an array of chars from a string constant
6628 optionally enclosed in braces. */
6629
6630 if (code == ARRAY_TYPE && inside_init
6631 && TREE_CODE (inside_init) == STRING_CST)
6632 {
6633 tree typ1
6634 = (TYPE_ATOMIC (TREE_TYPE (type))
6635 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6636 TYPE_QUAL_ATOMIC)
6637 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6638 /* Note that an array could be both an array of character type
6639 and an array of wchar_t if wchar_t is signed char or unsigned
6640 char. */
6641 bool char_array = (typ1 == char_type_node
6642 || typ1 == signed_char_type_node
6643 || typ1 == unsigned_char_type_node);
6644 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6645 bool char16_array = !!comptypes (typ1, char16_type_node);
6646 bool char32_array = !!comptypes (typ1, char32_type_node);
6647
6648 if (char_array || wchar_array || char16_array || char32_array)
6649 {
6650 struct c_expr expr;
6651 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6652 expr.value = inside_init;
6653 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6654 expr.original_type = NULL;
6655 maybe_warn_string_init (init_loc, type, expr);
6656
6657 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6658 pedwarn_init (init_loc, OPT_Wpedantic,
6659 "initialization of a flexible array member");
6660
6661 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6662 TYPE_MAIN_VARIANT (type)))
6663 return inside_init;
6664
6665 if (char_array)
6666 {
6667 if (typ2 != char_type_node)
6668 {
6669 error_init (init_loc, "char-array initialized from wide "
6670 "string");
6671 return error_mark_node;
6672 }
6673 }
6674 else
6675 {
6676 if (typ2 == char_type_node)
6677 {
6678 error_init (init_loc, "wide character array initialized "
6679 "from non-wide string");
6680 return error_mark_node;
6681 }
6682 else if (!comptypes(typ1, typ2))
6683 {
6684 error_init (init_loc, "wide character array initialized "
6685 "from incompatible wide string");
6686 return error_mark_node;
6687 }
6688 }
6689
6690 TREE_TYPE (inside_init) = type;
6691 if (TYPE_DOMAIN (type) != 0
6692 && TYPE_SIZE (type) != 0
6693 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6694 {
6695 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6696
6697 /* Subtract the size of a single (possibly wide) character
6698 because it's ok to ignore the terminating null char
6699 that is counted in the length of the constant. */
6700 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6701 (len
6702 - (TYPE_PRECISION (typ1)
6703 / BITS_PER_UNIT))))
6704 pedwarn_init (init_loc, 0,
6705 ("initializer-string for array of chars "
6706 "is too long"));
6707 else if (warn_cxx_compat
6708 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6709 warning_at (init_loc, OPT_Wc___compat,
6710 ("initializer-string for array chars "
6711 "is too long for C++"));
6712 }
6713
6714 return inside_init;
6715 }
6716 else if (INTEGRAL_TYPE_P (typ1))
6717 {
6718 error_init (init_loc, "array of inappropriate type initialized "
6719 "from string constant");
6720 return error_mark_node;
6721 }
6722 }
6723
6724 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6725 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6726 below and handle as a constructor. */
6727 if (code == VECTOR_TYPE
6728 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6729 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6730 && TREE_CONSTANT (inside_init))
6731 {
6732 if (TREE_CODE (inside_init) == VECTOR_CST
6733 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6734 TYPE_MAIN_VARIANT (type)))
6735 return inside_init;
6736
6737 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6738 {
6739 unsigned HOST_WIDE_INT ix;
6740 tree value;
6741 bool constant_p = true;
6742
6743 /* Iterate through elements and check if all constructor
6744 elements are *_CSTs. */
6745 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6746 if (!CONSTANT_CLASS_P (value))
6747 {
6748 constant_p = false;
6749 break;
6750 }
6751
6752 if (constant_p)
6753 return build_vector_from_ctor (type,
6754 CONSTRUCTOR_ELTS (inside_init));
6755 }
6756 }
6757
6758 if (warn_sequence_point)
6759 verify_sequence_points (inside_init);
6760
6761 /* Any type can be initialized
6762 from an expression of the same type, optionally with braces. */
6763
6764 if (inside_init && TREE_TYPE (inside_init) != 0
6765 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6766 TYPE_MAIN_VARIANT (type))
6767 || (code == ARRAY_TYPE
6768 && comptypes (TREE_TYPE (inside_init), type))
6769 || (code == VECTOR_TYPE
6770 && comptypes (TREE_TYPE (inside_init), type))
6771 || (code == POINTER_TYPE
6772 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6773 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6774 TREE_TYPE (type)))))
6775 {
6776 if (code == POINTER_TYPE)
6777 {
6778 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6779 {
6780 if (TREE_CODE (inside_init) == STRING_CST
6781 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6782 inside_init = array_to_pointer_conversion
6783 (init_loc, inside_init);
6784 else
6785 {
6786 error_init (init_loc, "invalid use of non-lvalue array");
6787 return error_mark_node;
6788 }
6789 }
6790 }
6791
6792 if (code == VECTOR_TYPE)
6793 /* Although the types are compatible, we may require a
6794 conversion. */
6795 inside_init = convert (type, inside_init);
6796
6797 if (require_constant
6798 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6799 {
6800 /* As an extension, allow initializing objects with static storage
6801 duration with compound literals (which are then treated just as
6802 the brace enclosed list they contain). Also allow this for
6803 vectors, as we can only assign them with compound literals. */
6804 if (flag_isoc99 && code != VECTOR_TYPE)
6805 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6806 "is not constant");
6807 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6808 inside_init = DECL_INITIAL (decl);
6809 }
6810
6811 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6812 && TREE_CODE (inside_init) != CONSTRUCTOR)
6813 {
6814 error_init (init_loc, "array initialized from non-constant array "
6815 "expression");
6816 return error_mark_node;
6817 }
6818
6819 /* Compound expressions can only occur here if -Wpedantic or
6820 -pedantic-errors is specified. In the later case, we always want
6821 an error. In the former case, we simply want a warning. */
6822 if (require_constant && pedantic
6823 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6824 {
6825 inside_init
6826 = valid_compound_expr_initializer (inside_init,
6827 TREE_TYPE (inside_init));
6828 if (inside_init == error_mark_node)
6829 error_init (init_loc, "initializer element is not constant");
6830 else
6831 pedwarn_init (init_loc, OPT_Wpedantic,
6832 "initializer element is not constant");
6833 if (flag_pedantic_errors)
6834 inside_init = error_mark_node;
6835 }
6836 else if (require_constant
6837 && !initializer_constant_valid_p (inside_init,
6838 TREE_TYPE (inside_init)))
6839 {
6840 error_init (init_loc, "initializer element is not constant");
6841 inside_init = error_mark_node;
6842 }
6843 else if (require_constant && !maybe_const)
6844 pedwarn_init (init_loc, 0,
6845 "initializer element is not a constant expression");
6846
6847 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6848 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6849 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6850 type, inside_init, origtype,
6851 ic_init, null_pointer_constant,
6852 NULL_TREE, NULL_TREE, 0);
6853 return inside_init;
6854 }
6855
6856 /* Handle scalar types, including conversions. */
6857
6858 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6859 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6860 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6861 {
6862 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6863 && (TREE_CODE (init) == STRING_CST
6864 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6865 inside_init = init = array_to_pointer_conversion (init_loc, init);
6866 if (semantic_type)
6867 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6868 inside_init);
6869 inside_init
6870 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6871 inside_init, origtype, ic_init,
6872 null_pointer_constant, NULL_TREE, NULL_TREE,
6873 0);
6874
6875 /* Check to see if we have already given an error message. */
6876 if (inside_init == error_mark_node)
6877 ;
6878 else if (require_constant && !TREE_CONSTANT (inside_init))
6879 {
6880 error_init (init_loc, "initializer element is not constant");
6881 inside_init = error_mark_node;
6882 }
6883 else if (require_constant
6884 && !initializer_constant_valid_p (inside_init,
6885 TREE_TYPE (inside_init)))
6886 {
6887 error_init (init_loc, "initializer element is not computable at "
6888 "load time");
6889 inside_init = error_mark_node;
6890 }
6891 else if (require_constant && !maybe_const)
6892 pedwarn_init (init_loc, 0,
6893 "initializer element is not a constant expression");
6894
6895 return inside_init;
6896 }
6897
6898 /* Come here only for records and arrays. */
6899
6900 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6901 {
6902 error_init (init_loc, "variable-sized object may not be initialized");
6903 return error_mark_node;
6904 }
6905
6906 error_init (init_loc, "invalid initializer");
6907 return error_mark_node;
6908 }
6909 \f
6910 /* Handle initializers that use braces. */
6911
6912 /* Type of object we are accumulating a constructor for.
6913 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6914 static tree constructor_type;
6915
6916 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6917 left to fill. */
6918 static tree constructor_fields;
6919
6920 /* For an ARRAY_TYPE, this is the specified index
6921 at which to store the next element we get. */
6922 static tree constructor_index;
6923
6924 /* For an ARRAY_TYPE, this is the maximum index. */
6925 static tree constructor_max_index;
6926
6927 /* For a RECORD_TYPE, this is the first field not yet written out. */
6928 static tree constructor_unfilled_fields;
6929
6930 /* For an ARRAY_TYPE, this is the index of the first element
6931 not yet written out. */
6932 static tree constructor_unfilled_index;
6933
6934 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6935 This is so we can generate gaps between fields, when appropriate. */
6936 static tree constructor_bit_index;
6937
6938 /* If we are saving up the elements rather than allocating them,
6939 this is the list of elements so far (in reverse order,
6940 most recent first). */
6941 static vec<constructor_elt, va_gc> *constructor_elements;
6942
6943 /* 1 if constructor should be incrementally stored into a constructor chain,
6944 0 if all the elements should be kept in AVL tree. */
6945 static int constructor_incremental;
6946
6947 /* 1 if so far this constructor's elements are all compile-time constants. */
6948 static int constructor_constant;
6949
6950 /* 1 if so far this constructor's elements are all valid address constants. */
6951 static int constructor_simple;
6952
6953 /* 1 if this constructor has an element that cannot be part of a
6954 constant expression. */
6955 static int constructor_nonconst;
6956
6957 /* 1 if this constructor is erroneous so far. */
6958 static int constructor_erroneous;
6959
6960 /* 1 if this constructor is the universal zero initializer { 0 }. */
6961 static int constructor_zeroinit;
6962
6963 /* Structure for managing pending initializer elements, organized as an
6964 AVL tree. */
6965
6966 struct init_node
6967 {
6968 struct init_node *left, *right;
6969 struct init_node *parent;
6970 int balance;
6971 tree purpose;
6972 tree value;
6973 tree origtype;
6974 };
6975
6976 /* Tree of pending elements at this constructor level.
6977 These are elements encountered out of order
6978 which belong at places we haven't reached yet in actually
6979 writing the output.
6980 Will never hold tree nodes across GC runs. */
6981 static struct init_node *constructor_pending_elts;
6982
6983 /* The SPELLING_DEPTH of this constructor. */
6984 static int constructor_depth;
6985
6986 /* DECL node for which an initializer is being read.
6987 0 means we are reading a constructor expression
6988 such as (struct foo) {...}. */
6989 static tree constructor_decl;
6990
6991 /* Nonzero if this is an initializer for a top-level decl. */
6992 static int constructor_top_level;
6993
6994 /* Nonzero if there were any member designators in this initializer. */
6995 static int constructor_designated;
6996
6997 /* Nesting depth of designator list. */
6998 static int designator_depth;
6999
7000 /* Nonzero if there were diagnosed errors in this designator list. */
7001 static int designator_erroneous;
7002
7003 \f
7004 /* This stack has a level for each implicit or explicit level of
7005 structuring in the initializer, including the outermost one. It
7006 saves the values of most of the variables above. */
7007
7008 struct constructor_range_stack;
7009
7010 struct constructor_stack
7011 {
7012 struct constructor_stack *next;
7013 tree type;
7014 tree fields;
7015 tree index;
7016 tree max_index;
7017 tree unfilled_index;
7018 tree unfilled_fields;
7019 tree bit_index;
7020 vec<constructor_elt, va_gc> *elements;
7021 struct init_node *pending_elts;
7022 int offset;
7023 int depth;
7024 /* If value nonzero, this value should replace the entire
7025 constructor at this level. */
7026 struct c_expr replacement_value;
7027 struct constructor_range_stack *range_stack;
7028 char constant;
7029 char simple;
7030 char nonconst;
7031 char implicit;
7032 char erroneous;
7033 char outer;
7034 char incremental;
7035 char designated;
7036 int designator_depth;
7037 };
7038
7039 static struct constructor_stack *constructor_stack;
7040
7041 /* This stack represents designators from some range designator up to
7042 the last designator in the list. */
7043
7044 struct constructor_range_stack
7045 {
7046 struct constructor_range_stack *next, *prev;
7047 struct constructor_stack *stack;
7048 tree range_start;
7049 tree index;
7050 tree range_end;
7051 tree fields;
7052 };
7053
7054 static struct constructor_range_stack *constructor_range_stack;
7055
7056 /* This stack records separate initializers that are nested.
7057 Nested initializers can't happen in ANSI C, but GNU C allows them
7058 in cases like { ... (struct foo) { ... } ... }. */
7059
7060 struct initializer_stack
7061 {
7062 struct initializer_stack *next;
7063 tree decl;
7064 struct constructor_stack *constructor_stack;
7065 struct constructor_range_stack *constructor_range_stack;
7066 vec<constructor_elt, va_gc> *elements;
7067 struct spelling *spelling;
7068 struct spelling *spelling_base;
7069 int spelling_size;
7070 char top_level;
7071 char require_constant_value;
7072 char require_constant_elements;
7073 };
7074
7075 static struct initializer_stack *initializer_stack;
7076 \f
7077 /* Prepare to parse and output the initializer for variable DECL. */
7078
7079 void
7080 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7081 {
7082 const char *locus;
7083 struct initializer_stack *p = XNEW (struct initializer_stack);
7084
7085 p->decl = constructor_decl;
7086 p->require_constant_value = require_constant_value;
7087 p->require_constant_elements = require_constant_elements;
7088 p->constructor_stack = constructor_stack;
7089 p->constructor_range_stack = constructor_range_stack;
7090 p->elements = constructor_elements;
7091 p->spelling = spelling;
7092 p->spelling_base = spelling_base;
7093 p->spelling_size = spelling_size;
7094 p->top_level = constructor_top_level;
7095 p->next = initializer_stack;
7096 initializer_stack = p;
7097
7098 constructor_decl = decl;
7099 constructor_designated = 0;
7100 constructor_top_level = top_level;
7101
7102 if (decl != 0 && decl != error_mark_node)
7103 {
7104 require_constant_value = TREE_STATIC (decl);
7105 require_constant_elements
7106 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7107 /* For a scalar, you can always use any value to initialize,
7108 even within braces. */
7109 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
7110 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
7111 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
7112 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
7113 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7114 }
7115 else
7116 {
7117 require_constant_value = 0;
7118 require_constant_elements = 0;
7119 locus = _("(anonymous)");
7120 }
7121
7122 constructor_stack = 0;
7123 constructor_range_stack = 0;
7124
7125 found_missing_braces = 0;
7126
7127 spelling_base = 0;
7128 spelling_size = 0;
7129 RESTORE_SPELLING_DEPTH (0);
7130
7131 if (locus)
7132 push_string (locus);
7133 }
7134
7135 void
7136 finish_init (void)
7137 {
7138 struct initializer_stack *p = initializer_stack;
7139
7140 /* Free the whole constructor stack of this initializer. */
7141 while (constructor_stack)
7142 {
7143 struct constructor_stack *q = constructor_stack;
7144 constructor_stack = q->next;
7145 free (q);
7146 }
7147
7148 gcc_assert (!constructor_range_stack);
7149
7150 /* Pop back to the data of the outer initializer (if any). */
7151 free (spelling_base);
7152
7153 constructor_decl = p->decl;
7154 require_constant_value = p->require_constant_value;
7155 require_constant_elements = p->require_constant_elements;
7156 constructor_stack = p->constructor_stack;
7157 constructor_range_stack = p->constructor_range_stack;
7158 constructor_elements = p->elements;
7159 spelling = p->spelling;
7160 spelling_base = p->spelling_base;
7161 spelling_size = p->spelling_size;
7162 constructor_top_level = p->top_level;
7163 initializer_stack = p->next;
7164 free (p);
7165 }
7166 \f
7167 /* Call here when we see the initializer is surrounded by braces.
7168 This is instead of a call to push_init_level;
7169 it is matched by a call to pop_init_level.
7170
7171 TYPE is the type to initialize, for a constructor expression.
7172 For an initializer for a decl, TYPE is zero. */
7173
7174 void
7175 really_start_incremental_init (tree type)
7176 {
7177 struct constructor_stack *p = XNEW (struct constructor_stack);
7178
7179 if (type == 0)
7180 type = TREE_TYPE (constructor_decl);
7181
7182 if (TREE_CODE (type) == VECTOR_TYPE
7183 && TYPE_VECTOR_OPAQUE (type))
7184 error ("opaque vector types cannot be initialized");
7185
7186 p->type = constructor_type;
7187 p->fields = constructor_fields;
7188 p->index = constructor_index;
7189 p->max_index = constructor_max_index;
7190 p->unfilled_index = constructor_unfilled_index;
7191 p->unfilled_fields = constructor_unfilled_fields;
7192 p->bit_index = constructor_bit_index;
7193 p->elements = constructor_elements;
7194 p->constant = constructor_constant;
7195 p->simple = constructor_simple;
7196 p->nonconst = constructor_nonconst;
7197 p->erroneous = constructor_erroneous;
7198 p->pending_elts = constructor_pending_elts;
7199 p->depth = constructor_depth;
7200 p->replacement_value.value = 0;
7201 p->replacement_value.original_code = ERROR_MARK;
7202 p->replacement_value.original_type = NULL;
7203 p->implicit = 0;
7204 p->range_stack = 0;
7205 p->outer = 0;
7206 p->incremental = constructor_incremental;
7207 p->designated = constructor_designated;
7208 p->designator_depth = designator_depth;
7209 p->next = 0;
7210 constructor_stack = p;
7211
7212 constructor_constant = 1;
7213 constructor_simple = 1;
7214 constructor_nonconst = 0;
7215 constructor_depth = SPELLING_DEPTH ();
7216 constructor_elements = NULL;
7217 constructor_pending_elts = 0;
7218 constructor_type = type;
7219 constructor_incremental = 1;
7220 constructor_designated = 0;
7221 constructor_zeroinit = 1;
7222 designator_depth = 0;
7223 designator_erroneous = 0;
7224
7225 if (TREE_CODE (constructor_type) == RECORD_TYPE
7226 || TREE_CODE (constructor_type) == UNION_TYPE)
7227 {
7228 constructor_fields = TYPE_FIELDS (constructor_type);
7229 /* Skip any nameless bit fields at the beginning. */
7230 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7231 && DECL_NAME (constructor_fields) == 0)
7232 constructor_fields = DECL_CHAIN (constructor_fields);
7233
7234 constructor_unfilled_fields = constructor_fields;
7235 constructor_bit_index = bitsize_zero_node;
7236 }
7237 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7238 {
7239 if (TYPE_DOMAIN (constructor_type))
7240 {
7241 constructor_max_index
7242 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7243
7244 /* Detect non-empty initializations of zero-length arrays. */
7245 if (constructor_max_index == NULL_TREE
7246 && TYPE_SIZE (constructor_type))
7247 constructor_max_index = integer_minus_one_node;
7248
7249 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7250 to initialize VLAs will cause a proper error; avoid tree
7251 checking errors as well by setting a safe value. */
7252 if (constructor_max_index
7253 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7254 constructor_max_index = integer_minus_one_node;
7255
7256 constructor_index
7257 = convert (bitsizetype,
7258 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7259 }
7260 else
7261 {
7262 constructor_index = bitsize_zero_node;
7263 constructor_max_index = NULL_TREE;
7264 }
7265
7266 constructor_unfilled_index = constructor_index;
7267 }
7268 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7269 {
7270 /* Vectors are like simple fixed-size arrays. */
7271 constructor_max_index =
7272 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7273 constructor_index = bitsize_zero_node;
7274 constructor_unfilled_index = constructor_index;
7275 }
7276 else
7277 {
7278 /* Handle the case of int x = {5}; */
7279 constructor_fields = constructor_type;
7280 constructor_unfilled_fields = constructor_type;
7281 }
7282 }
7283 \f
7284 /* Push down into a subobject, for initialization.
7285 If this is for an explicit set of braces, IMPLICIT is 0.
7286 If it is because the next element belongs at a lower level,
7287 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7288
7289 void
7290 push_init_level (location_t loc, int implicit,
7291 struct obstack *braced_init_obstack)
7292 {
7293 struct constructor_stack *p;
7294 tree value = NULL_TREE;
7295
7296 /* If we've exhausted any levels that didn't have braces,
7297 pop them now. If implicit == 1, this will have been done in
7298 process_init_element; do not repeat it here because in the case
7299 of excess initializers for an empty aggregate this leads to an
7300 infinite cycle of popping a level and immediately recreating
7301 it. */
7302 if (implicit != 1)
7303 {
7304 while (constructor_stack->implicit)
7305 {
7306 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7307 || TREE_CODE (constructor_type) == UNION_TYPE)
7308 && constructor_fields == 0)
7309 process_init_element (input_location,
7310 pop_init_level (loc, 1, braced_init_obstack),
7311 true, braced_init_obstack);
7312 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7313 && constructor_max_index
7314 && tree_int_cst_lt (constructor_max_index,
7315 constructor_index))
7316 process_init_element (input_location,
7317 pop_init_level (loc, 1, braced_init_obstack),
7318 true, braced_init_obstack);
7319 else
7320 break;
7321 }
7322 }
7323
7324 /* Unless this is an explicit brace, we need to preserve previous
7325 content if any. */
7326 if (implicit)
7327 {
7328 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7329 || TREE_CODE (constructor_type) == UNION_TYPE)
7330 && constructor_fields)
7331 value = find_init_member (constructor_fields, braced_init_obstack);
7332 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7333 value = find_init_member (constructor_index, braced_init_obstack);
7334 }
7335
7336 p = XNEW (struct constructor_stack);
7337 p->type = constructor_type;
7338 p->fields = constructor_fields;
7339 p->index = constructor_index;
7340 p->max_index = constructor_max_index;
7341 p->unfilled_index = constructor_unfilled_index;
7342 p->unfilled_fields = constructor_unfilled_fields;
7343 p->bit_index = constructor_bit_index;
7344 p->elements = constructor_elements;
7345 p->constant = constructor_constant;
7346 p->simple = constructor_simple;
7347 p->nonconst = constructor_nonconst;
7348 p->erroneous = constructor_erroneous;
7349 p->pending_elts = constructor_pending_elts;
7350 p->depth = constructor_depth;
7351 p->replacement_value.value = 0;
7352 p->replacement_value.original_code = ERROR_MARK;
7353 p->replacement_value.original_type = NULL;
7354 p->implicit = implicit;
7355 p->outer = 0;
7356 p->incremental = constructor_incremental;
7357 p->designated = constructor_designated;
7358 p->designator_depth = designator_depth;
7359 p->next = constructor_stack;
7360 p->range_stack = 0;
7361 constructor_stack = p;
7362
7363 constructor_constant = 1;
7364 constructor_simple = 1;
7365 constructor_nonconst = 0;
7366 constructor_depth = SPELLING_DEPTH ();
7367 constructor_elements = NULL;
7368 constructor_incremental = 1;
7369 constructor_designated = 0;
7370 constructor_pending_elts = 0;
7371 if (!implicit)
7372 {
7373 p->range_stack = constructor_range_stack;
7374 constructor_range_stack = 0;
7375 designator_depth = 0;
7376 designator_erroneous = 0;
7377 }
7378
7379 /* Don't die if an entire brace-pair level is superfluous
7380 in the containing level. */
7381 if (constructor_type == 0)
7382 ;
7383 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7384 || TREE_CODE (constructor_type) == UNION_TYPE)
7385 {
7386 /* Don't die if there are extra init elts at the end. */
7387 if (constructor_fields == 0)
7388 constructor_type = 0;
7389 else
7390 {
7391 constructor_type = TREE_TYPE (constructor_fields);
7392 push_member_name (constructor_fields);
7393 constructor_depth++;
7394 }
7395 /* If upper initializer is designated, then mark this as
7396 designated too to prevent bogus warnings. */
7397 constructor_designated = p->designated;
7398 }
7399 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7400 {
7401 constructor_type = TREE_TYPE (constructor_type);
7402 push_array_bounds (tree_to_uhwi (constructor_index));
7403 constructor_depth++;
7404 }
7405
7406 if (constructor_type == 0)
7407 {
7408 error_init (loc, "extra brace group at end of initializer");
7409 constructor_fields = 0;
7410 constructor_unfilled_fields = 0;
7411 return;
7412 }
7413
7414 if (value && TREE_CODE (value) == CONSTRUCTOR)
7415 {
7416 constructor_constant = TREE_CONSTANT (value);
7417 constructor_simple = TREE_STATIC (value);
7418 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7419 constructor_elements = CONSTRUCTOR_ELTS (value);
7420 if (!vec_safe_is_empty (constructor_elements)
7421 && (TREE_CODE (constructor_type) == RECORD_TYPE
7422 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7423 set_nonincremental_init (braced_init_obstack);
7424 }
7425
7426 if (implicit == 1)
7427 found_missing_braces = 1;
7428
7429 if (TREE_CODE (constructor_type) == RECORD_TYPE
7430 || TREE_CODE (constructor_type) == UNION_TYPE)
7431 {
7432 constructor_fields = TYPE_FIELDS (constructor_type);
7433 /* Skip any nameless bit fields at the beginning. */
7434 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7435 && DECL_NAME (constructor_fields) == 0)
7436 constructor_fields = DECL_CHAIN (constructor_fields);
7437
7438 constructor_unfilled_fields = constructor_fields;
7439 constructor_bit_index = bitsize_zero_node;
7440 }
7441 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7442 {
7443 /* Vectors are like simple fixed-size arrays. */
7444 constructor_max_index =
7445 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7446 constructor_index = bitsize_int (0);
7447 constructor_unfilled_index = constructor_index;
7448 }
7449 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7450 {
7451 if (TYPE_DOMAIN (constructor_type))
7452 {
7453 constructor_max_index
7454 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7455
7456 /* Detect non-empty initializations of zero-length arrays. */
7457 if (constructor_max_index == NULL_TREE
7458 && TYPE_SIZE (constructor_type))
7459 constructor_max_index = integer_minus_one_node;
7460
7461 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7462 to initialize VLAs will cause a proper error; avoid tree
7463 checking errors as well by setting a safe value. */
7464 if (constructor_max_index
7465 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7466 constructor_max_index = integer_minus_one_node;
7467
7468 constructor_index
7469 = convert (bitsizetype,
7470 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7471 }
7472 else
7473 constructor_index = bitsize_zero_node;
7474
7475 constructor_unfilled_index = constructor_index;
7476 if (value && TREE_CODE (value) == STRING_CST)
7477 {
7478 /* We need to split the char/wchar array into individual
7479 characters, so that we don't have to special case it
7480 everywhere. */
7481 set_nonincremental_init_from_string (value, braced_init_obstack);
7482 }
7483 }
7484 else
7485 {
7486 if (constructor_type != error_mark_node)
7487 warning_init (input_location, 0, "braces around scalar initializer");
7488 constructor_fields = constructor_type;
7489 constructor_unfilled_fields = constructor_type;
7490 }
7491 }
7492
7493 /* At the end of an implicit or explicit brace level,
7494 finish up that level of constructor. If a single expression
7495 with redundant braces initialized that level, return the
7496 c_expr structure for that expression. Otherwise, the original_code
7497 element is set to ERROR_MARK.
7498 If we were outputting the elements as they are read, return 0 as the value
7499 from inner levels (process_init_element ignores that),
7500 but return error_mark_node as the value from the outermost level
7501 (that's what we want to put in DECL_INITIAL).
7502 Otherwise, return a CONSTRUCTOR expression as the value. */
7503
7504 struct c_expr
7505 pop_init_level (location_t loc, int implicit,
7506 struct obstack *braced_init_obstack)
7507 {
7508 struct constructor_stack *p;
7509 struct c_expr ret;
7510 ret.value = 0;
7511 ret.original_code = ERROR_MARK;
7512 ret.original_type = NULL;
7513
7514 if (implicit == 0)
7515 {
7516 /* When we come to an explicit close brace,
7517 pop any inner levels that didn't have explicit braces. */
7518 while (constructor_stack->implicit)
7519 process_init_element (input_location,
7520 pop_init_level (loc, 1, braced_init_obstack),
7521 true, braced_init_obstack);
7522 gcc_assert (!constructor_range_stack);
7523 }
7524
7525 /* Now output all pending elements. */
7526 constructor_incremental = 1;
7527 output_pending_init_elements (1, braced_init_obstack);
7528
7529 p = constructor_stack;
7530
7531 /* Error for initializing a flexible array member, or a zero-length
7532 array member in an inappropriate context. */
7533 if (constructor_type && constructor_fields
7534 && TREE_CODE (constructor_type) == ARRAY_TYPE
7535 && TYPE_DOMAIN (constructor_type)
7536 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7537 {
7538 /* Silently discard empty initializations. The parser will
7539 already have pedwarned for empty brackets. */
7540 if (integer_zerop (constructor_unfilled_index))
7541 constructor_type = NULL_TREE;
7542 else
7543 {
7544 gcc_assert (!TYPE_SIZE (constructor_type));
7545
7546 if (constructor_depth > 2)
7547 error_init (loc, "initialization of flexible array member in a nested context");
7548 else
7549 pedwarn_init (loc, OPT_Wpedantic,
7550 "initialization of a flexible array member");
7551
7552 /* We have already issued an error message for the existence
7553 of a flexible array member not at the end of the structure.
7554 Discard the initializer so that we do not die later. */
7555 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7556 constructor_type = NULL_TREE;
7557 }
7558 }
7559
7560 switch (vec_safe_length (constructor_elements))
7561 {
7562 case 0:
7563 /* Initialization with { } counts as zeroinit. */
7564 constructor_zeroinit = 1;
7565 break;
7566 case 1:
7567 /* This might be zeroinit as well. */
7568 if (integer_zerop ((*constructor_elements)[0].value))
7569 constructor_zeroinit = 1;
7570 break;
7571 default:
7572 /* If the constructor has more than one element, it can't be { 0 }. */
7573 constructor_zeroinit = 0;
7574 break;
7575 }
7576
7577 /* Warn when some structs are initialized with direct aggregation. */
7578 if (!implicit && found_missing_braces && warn_missing_braces
7579 && !constructor_zeroinit)
7580 warning_init (loc, OPT_Wmissing_braces,
7581 "missing braces around initializer");
7582
7583 /* Warn when some struct elements are implicitly initialized to zero. */
7584 if (warn_missing_field_initializers
7585 && constructor_type
7586 && TREE_CODE (constructor_type) == RECORD_TYPE
7587 && constructor_unfilled_fields)
7588 {
7589 /* Do not warn for flexible array members or zero-length arrays. */
7590 while (constructor_unfilled_fields
7591 && (!DECL_SIZE (constructor_unfilled_fields)
7592 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7593 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7594
7595 if (constructor_unfilled_fields
7596 /* Do not warn if this level of the initializer uses member
7597 designators; it is likely to be deliberate. */
7598 && !constructor_designated
7599 /* Do not warn about initializing with { 0 } or with { }. */
7600 && !constructor_zeroinit)
7601 {
7602 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7603 "missing initializer for field %qD of %qT",
7604 constructor_unfilled_fields,
7605 constructor_type))
7606 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7607 "%qD declared here", constructor_unfilled_fields);
7608 }
7609 }
7610
7611 /* Pad out the end of the structure. */
7612 if (p->replacement_value.value)
7613 /* If this closes a superfluous brace pair,
7614 just pass out the element between them. */
7615 ret = p->replacement_value;
7616 else if (constructor_type == 0)
7617 ;
7618 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7619 && TREE_CODE (constructor_type) != UNION_TYPE
7620 && TREE_CODE (constructor_type) != ARRAY_TYPE
7621 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7622 {
7623 /* A nonincremental scalar initializer--just return
7624 the element, after verifying there is just one. */
7625 if (vec_safe_is_empty (constructor_elements))
7626 {
7627 if (!constructor_erroneous)
7628 error_init (loc, "empty scalar initializer");
7629 ret.value = error_mark_node;
7630 }
7631 else if (vec_safe_length (constructor_elements) != 1)
7632 {
7633 error_init (loc, "extra elements in scalar initializer");
7634 ret.value = (*constructor_elements)[0].value;
7635 }
7636 else
7637 ret.value = (*constructor_elements)[0].value;
7638 }
7639 else
7640 {
7641 if (constructor_erroneous)
7642 ret.value = error_mark_node;
7643 else
7644 {
7645 ret.value = build_constructor (constructor_type,
7646 constructor_elements);
7647 if (constructor_constant)
7648 TREE_CONSTANT (ret.value) = 1;
7649 if (constructor_constant && constructor_simple)
7650 TREE_STATIC (ret.value) = 1;
7651 if (constructor_nonconst)
7652 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7653 }
7654 }
7655
7656 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7657 {
7658 if (constructor_nonconst)
7659 ret.original_code = C_MAYBE_CONST_EXPR;
7660 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7661 ret.original_code = ERROR_MARK;
7662 }
7663
7664 constructor_type = p->type;
7665 constructor_fields = p->fields;
7666 constructor_index = p->index;
7667 constructor_max_index = p->max_index;
7668 constructor_unfilled_index = p->unfilled_index;
7669 constructor_unfilled_fields = p->unfilled_fields;
7670 constructor_bit_index = p->bit_index;
7671 constructor_elements = p->elements;
7672 constructor_constant = p->constant;
7673 constructor_simple = p->simple;
7674 constructor_nonconst = p->nonconst;
7675 constructor_erroneous = p->erroneous;
7676 constructor_incremental = p->incremental;
7677 constructor_designated = p->designated;
7678 designator_depth = p->designator_depth;
7679 constructor_pending_elts = p->pending_elts;
7680 constructor_depth = p->depth;
7681 if (!p->implicit)
7682 constructor_range_stack = p->range_stack;
7683 RESTORE_SPELLING_DEPTH (constructor_depth);
7684
7685 constructor_stack = p->next;
7686 free (p);
7687
7688 if (ret.value == 0 && constructor_stack == 0)
7689 ret.value = error_mark_node;
7690 return ret;
7691 }
7692
7693 /* Common handling for both array range and field name designators.
7694 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7695
7696 static int
7697 set_designator (location_t loc, int array,
7698 struct obstack *braced_init_obstack)
7699 {
7700 tree subtype;
7701 enum tree_code subcode;
7702
7703 /* Don't die if an entire brace-pair level is superfluous
7704 in the containing level. */
7705 if (constructor_type == 0)
7706 return 1;
7707
7708 /* If there were errors in this designator list already, bail out
7709 silently. */
7710 if (designator_erroneous)
7711 return 1;
7712
7713 if (!designator_depth)
7714 {
7715 gcc_assert (!constructor_range_stack);
7716
7717 /* Designator list starts at the level of closest explicit
7718 braces. */
7719 while (constructor_stack->implicit)
7720 process_init_element (input_location,
7721 pop_init_level (loc, 1, braced_init_obstack),
7722 true, braced_init_obstack);
7723 constructor_designated = 1;
7724 return 0;
7725 }
7726
7727 switch (TREE_CODE (constructor_type))
7728 {
7729 case RECORD_TYPE:
7730 case UNION_TYPE:
7731 subtype = TREE_TYPE (constructor_fields);
7732 if (subtype != error_mark_node)
7733 subtype = TYPE_MAIN_VARIANT (subtype);
7734 break;
7735 case ARRAY_TYPE:
7736 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7737 break;
7738 default:
7739 gcc_unreachable ();
7740 }
7741
7742 subcode = TREE_CODE (subtype);
7743 if (array && subcode != ARRAY_TYPE)
7744 {
7745 error_init (loc, "array index in non-array initializer");
7746 return 1;
7747 }
7748 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7749 {
7750 error_init (loc, "field name not in record or union initializer");
7751 return 1;
7752 }
7753
7754 constructor_designated = 1;
7755 push_init_level (loc, 2, braced_init_obstack);
7756 return 0;
7757 }
7758
7759 /* If there are range designators in designator list, push a new designator
7760 to constructor_range_stack. RANGE_END is end of such stack range or
7761 NULL_TREE if there is no range designator at this level. */
7762
7763 static void
7764 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7765 {
7766 struct constructor_range_stack *p;
7767
7768 p = (struct constructor_range_stack *)
7769 obstack_alloc (braced_init_obstack,
7770 sizeof (struct constructor_range_stack));
7771 p->prev = constructor_range_stack;
7772 p->next = 0;
7773 p->fields = constructor_fields;
7774 p->range_start = constructor_index;
7775 p->index = constructor_index;
7776 p->stack = constructor_stack;
7777 p->range_end = range_end;
7778 if (constructor_range_stack)
7779 constructor_range_stack->next = p;
7780 constructor_range_stack = p;
7781 }
7782
7783 /* Within an array initializer, specify the next index to be initialized.
7784 FIRST is that index. If LAST is nonzero, then initialize a range
7785 of indices, running from FIRST through LAST. */
7786
7787 void
7788 set_init_index (location_t loc, tree first, tree last,
7789 struct obstack *braced_init_obstack)
7790 {
7791 if (set_designator (loc, 1, braced_init_obstack))
7792 return;
7793
7794 designator_erroneous = 1;
7795
7796 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7797 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7798 {
7799 error_init (loc, "array index in initializer not of integer type");
7800 return;
7801 }
7802
7803 if (TREE_CODE (first) != INTEGER_CST)
7804 {
7805 first = c_fully_fold (first, false, NULL);
7806 if (TREE_CODE (first) == INTEGER_CST)
7807 pedwarn_init (loc, OPT_Wpedantic,
7808 "array index in initializer is not "
7809 "an integer constant expression");
7810 }
7811
7812 if (last && TREE_CODE (last) != INTEGER_CST)
7813 {
7814 last = c_fully_fold (last, false, NULL);
7815 if (TREE_CODE (last) == INTEGER_CST)
7816 pedwarn_init (loc, OPT_Wpedantic,
7817 "array index in initializer is not "
7818 "an integer constant expression");
7819 }
7820
7821 if (TREE_CODE (first) != INTEGER_CST)
7822 error_init (loc, "nonconstant array index in initializer");
7823 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7824 error_init (loc, "nonconstant array index in initializer");
7825 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7826 error_init (loc, "array index in non-array initializer");
7827 else if (tree_int_cst_sgn (first) == -1)
7828 error_init (loc, "array index in initializer exceeds array bounds");
7829 else if (constructor_max_index
7830 && tree_int_cst_lt (constructor_max_index, first))
7831 error_init (loc, "array index in initializer exceeds array bounds");
7832 else
7833 {
7834 constant_expression_warning (first);
7835 if (last)
7836 constant_expression_warning (last);
7837 constructor_index = convert (bitsizetype, first);
7838 if (tree_int_cst_lt (constructor_index, first))
7839 {
7840 constructor_index = copy_node (constructor_index);
7841 TREE_OVERFLOW (constructor_index) = 1;
7842 }
7843
7844 if (last)
7845 {
7846 if (tree_int_cst_equal (first, last))
7847 last = 0;
7848 else if (tree_int_cst_lt (last, first))
7849 {
7850 error_init (loc, "empty index range in initializer");
7851 last = 0;
7852 }
7853 else
7854 {
7855 last = convert (bitsizetype, last);
7856 if (constructor_max_index != 0
7857 && tree_int_cst_lt (constructor_max_index, last))
7858 {
7859 error_init (loc, "array index range in initializer exceeds "
7860 "array bounds");
7861 last = 0;
7862 }
7863 }
7864 }
7865
7866 designator_depth++;
7867 designator_erroneous = 0;
7868 if (constructor_range_stack || last)
7869 push_range_stack (last, braced_init_obstack);
7870 }
7871 }
7872
7873 /* Within a struct initializer, specify the next field to be initialized. */
7874
7875 void
7876 set_init_label (location_t loc, tree fieldname,
7877 struct obstack *braced_init_obstack)
7878 {
7879 tree field;
7880
7881 if (set_designator (loc, 0, braced_init_obstack))
7882 return;
7883
7884 designator_erroneous = 1;
7885
7886 if (TREE_CODE (constructor_type) != RECORD_TYPE
7887 && TREE_CODE (constructor_type) != UNION_TYPE)
7888 {
7889 error_init (loc, "field name not in record or union initializer");
7890 return;
7891 }
7892
7893 field = lookup_field (constructor_type, fieldname);
7894
7895 if (field == 0)
7896 error ("unknown field %qE specified in initializer", fieldname);
7897 else
7898 do
7899 {
7900 constructor_fields = TREE_VALUE (field);
7901 designator_depth++;
7902 designator_erroneous = 0;
7903 if (constructor_range_stack)
7904 push_range_stack (NULL_TREE, braced_init_obstack);
7905 field = TREE_CHAIN (field);
7906 if (field)
7907 {
7908 if (set_designator (loc, 0, braced_init_obstack))
7909 return;
7910 }
7911 }
7912 while (field != NULL_TREE);
7913 }
7914 \f
7915 /* Add a new initializer to the tree of pending initializers. PURPOSE
7916 identifies the initializer, either array index or field in a structure.
7917 VALUE is the value of that index or field. If ORIGTYPE is not
7918 NULL_TREE, it is the original type of VALUE.
7919
7920 IMPLICIT is true if value comes from pop_init_level (1),
7921 the new initializer has been merged with the existing one
7922 and thus no warnings should be emitted about overriding an
7923 existing initializer. */
7924
7925 static void
7926 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7927 bool implicit, struct obstack *braced_init_obstack)
7928 {
7929 struct init_node *p, **q, *r;
7930
7931 q = &constructor_pending_elts;
7932 p = 0;
7933
7934 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7935 {
7936 while (*q != 0)
7937 {
7938 p = *q;
7939 if (tree_int_cst_lt (purpose, p->purpose))
7940 q = &p->left;
7941 else if (tree_int_cst_lt (p->purpose, purpose))
7942 q = &p->right;
7943 else
7944 {
7945 if (!implicit)
7946 {
7947 if (TREE_SIDE_EFFECTS (p->value))
7948 warning_init (loc, 0,
7949 "initialized field with side-effects "
7950 "overwritten");
7951 else if (warn_override_init)
7952 warning_init (loc, OPT_Woverride_init,
7953 "initialized field overwritten");
7954 }
7955 p->value = value;
7956 p->origtype = origtype;
7957 return;
7958 }
7959 }
7960 }
7961 else
7962 {
7963 tree bitpos;
7964
7965 bitpos = bit_position (purpose);
7966 while (*q != NULL)
7967 {
7968 p = *q;
7969 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7970 q = &p->left;
7971 else if (p->purpose != purpose)
7972 q = &p->right;
7973 else
7974 {
7975 if (!implicit)
7976 {
7977 if (TREE_SIDE_EFFECTS (p->value))
7978 warning_init (loc, 0,
7979 "initialized field with side-effects "
7980 "overwritten");
7981 else if (warn_override_init)
7982 warning_init (loc, OPT_Woverride_init,
7983 "initialized field overwritten");
7984 }
7985 p->value = value;
7986 p->origtype = origtype;
7987 return;
7988 }
7989 }
7990 }
7991
7992 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7993 sizeof (struct init_node));
7994 r->purpose = purpose;
7995 r->value = value;
7996 r->origtype = origtype;
7997
7998 *q = r;
7999 r->parent = p;
8000 r->left = 0;
8001 r->right = 0;
8002 r->balance = 0;
8003
8004 while (p)
8005 {
8006 struct init_node *s;
8007
8008 if (r == p->left)
8009 {
8010 if (p->balance == 0)
8011 p->balance = -1;
8012 else if (p->balance < 0)
8013 {
8014 if (r->balance < 0)
8015 {
8016 /* L rotation. */
8017 p->left = r->right;
8018 if (p->left)
8019 p->left->parent = p;
8020 r->right = p;
8021
8022 p->balance = 0;
8023 r->balance = 0;
8024
8025 s = p->parent;
8026 p->parent = r;
8027 r->parent = s;
8028 if (s)
8029 {
8030 if (s->left == p)
8031 s->left = r;
8032 else
8033 s->right = r;
8034 }
8035 else
8036 constructor_pending_elts = r;
8037 }
8038 else
8039 {
8040 /* LR rotation. */
8041 struct init_node *t = r->right;
8042
8043 r->right = t->left;
8044 if (r->right)
8045 r->right->parent = r;
8046 t->left = r;
8047
8048 p->left = t->right;
8049 if (p->left)
8050 p->left->parent = p;
8051 t->right = p;
8052
8053 p->balance = t->balance < 0;
8054 r->balance = -(t->balance > 0);
8055 t->balance = 0;
8056
8057 s = p->parent;
8058 p->parent = t;
8059 r->parent = t;
8060 t->parent = s;
8061 if (s)
8062 {
8063 if (s->left == p)
8064 s->left = t;
8065 else
8066 s->right = t;
8067 }
8068 else
8069 constructor_pending_elts = t;
8070 }
8071 break;
8072 }
8073 else
8074 {
8075 /* p->balance == +1; growth of left side balances the node. */
8076 p->balance = 0;
8077 break;
8078 }
8079 }
8080 else /* r == p->right */
8081 {
8082 if (p->balance == 0)
8083 /* Growth propagation from right side. */
8084 p->balance++;
8085 else if (p->balance > 0)
8086 {
8087 if (r->balance > 0)
8088 {
8089 /* R rotation. */
8090 p->right = r->left;
8091 if (p->right)
8092 p->right->parent = p;
8093 r->left = p;
8094
8095 p->balance = 0;
8096 r->balance = 0;
8097
8098 s = p->parent;
8099 p->parent = r;
8100 r->parent = s;
8101 if (s)
8102 {
8103 if (s->left == p)
8104 s->left = r;
8105 else
8106 s->right = r;
8107 }
8108 else
8109 constructor_pending_elts = r;
8110 }
8111 else /* r->balance == -1 */
8112 {
8113 /* RL rotation */
8114 struct init_node *t = r->left;
8115
8116 r->left = t->right;
8117 if (r->left)
8118 r->left->parent = r;
8119 t->right = r;
8120
8121 p->right = t->left;
8122 if (p->right)
8123 p->right->parent = p;
8124 t->left = p;
8125
8126 r->balance = (t->balance < 0);
8127 p->balance = -(t->balance > 0);
8128 t->balance = 0;
8129
8130 s = p->parent;
8131 p->parent = t;
8132 r->parent = t;
8133 t->parent = s;
8134 if (s)
8135 {
8136 if (s->left == p)
8137 s->left = t;
8138 else
8139 s->right = t;
8140 }
8141 else
8142 constructor_pending_elts = t;
8143 }
8144 break;
8145 }
8146 else
8147 {
8148 /* p->balance == -1; growth of right side balances the node. */
8149 p->balance = 0;
8150 break;
8151 }
8152 }
8153
8154 r = p;
8155 p = p->parent;
8156 }
8157 }
8158
8159 /* Build AVL tree from a sorted chain. */
8160
8161 static void
8162 set_nonincremental_init (struct obstack * braced_init_obstack)
8163 {
8164 unsigned HOST_WIDE_INT ix;
8165 tree index, value;
8166
8167 if (TREE_CODE (constructor_type) != RECORD_TYPE
8168 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8169 return;
8170
8171 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8172 add_pending_init (input_location, index, value, NULL_TREE, true,
8173 braced_init_obstack);
8174 constructor_elements = NULL;
8175 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8176 {
8177 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8178 /* Skip any nameless bit fields at the beginning. */
8179 while (constructor_unfilled_fields != 0
8180 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8181 && DECL_NAME (constructor_unfilled_fields) == 0)
8182 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8183
8184 }
8185 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8186 {
8187 if (TYPE_DOMAIN (constructor_type))
8188 constructor_unfilled_index
8189 = convert (bitsizetype,
8190 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8191 else
8192 constructor_unfilled_index = bitsize_zero_node;
8193 }
8194 constructor_incremental = 0;
8195 }
8196
8197 /* Build AVL tree from a string constant. */
8198
8199 static void
8200 set_nonincremental_init_from_string (tree str,
8201 struct obstack * braced_init_obstack)
8202 {
8203 tree value, purpose, type;
8204 HOST_WIDE_INT val[2];
8205 const char *p, *end;
8206 int byte, wchar_bytes, charwidth, bitpos;
8207
8208 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8209
8210 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8211 charwidth = TYPE_PRECISION (char_type_node);
8212 type = TREE_TYPE (constructor_type);
8213 p = TREE_STRING_POINTER (str);
8214 end = p + TREE_STRING_LENGTH (str);
8215
8216 for (purpose = bitsize_zero_node;
8217 p < end
8218 && !(constructor_max_index
8219 && tree_int_cst_lt (constructor_max_index, purpose));
8220 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8221 {
8222 if (wchar_bytes == 1)
8223 {
8224 val[0] = (unsigned char) *p++;
8225 val[1] = 0;
8226 }
8227 else
8228 {
8229 val[1] = 0;
8230 val[0] = 0;
8231 for (byte = 0; byte < wchar_bytes; byte++)
8232 {
8233 if (BYTES_BIG_ENDIAN)
8234 bitpos = (wchar_bytes - byte - 1) * charwidth;
8235 else
8236 bitpos = byte * charwidth;
8237 val[bitpos % HOST_BITS_PER_WIDE_INT]
8238 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8239 << (bitpos % HOST_BITS_PER_WIDE_INT);
8240 }
8241 }
8242
8243 if (!TYPE_UNSIGNED (type))
8244 {
8245 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8246 if (bitpos < HOST_BITS_PER_WIDE_INT)
8247 {
8248 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8249 {
8250 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8251 val[1] = -1;
8252 }
8253 }
8254 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8255 {
8256 if (val[0] < 0)
8257 val[1] = -1;
8258 }
8259 else if (val[1] & (((HOST_WIDE_INT) 1)
8260 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8261 val[1] |= ((HOST_WIDE_INT) -1)
8262 << (bitpos - HOST_BITS_PER_WIDE_INT);
8263 }
8264
8265 value = wide_int_to_tree (type,
8266 wide_int::from_array (val, 2,
8267 HOST_BITS_PER_WIDE_INT * 2));
8268 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8269 braced_init_obstack);
8270 }
8271
8272 constructor_incremental = 0;
8273 }
8274
8275 /* Return value of FIELD in pending initializer or zero if the field was
8276 not initialized yet. */
8277
8278 static tree
8279 find_init_member (tree field, struct obstack * braced_init_obstack)
8280 {
8281 struct init_node *p;
8282
8283 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8284 {
8285 if (constructor_incremental
8286 && tree_int_cst_lt (field, constructor_unfilled_index))
8287 set_nonincremental_init (braced_init_obstack);
8288
8289 p = constructor_pending_elts;
8290 while (p)
8291 {
8292 if (tree_int_cst_lt (field, p->purpose))
8293 p = p->left;
8294 else if (tree_int_cst_lt (p->purpose, field))
8295 p = p->right;
8296 else
8297 return p->value;
8298 }
8299 }
8300 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8301 {
8302 tree bitpos = bit_position (field);
8303
8304 if (constructor_incremental
8305 && (!constructor_unfilled_fields
8306 || tree_int_cst_lt (bitpos,
8307 bit_position (constructor_unfilled_fields))))
8308 set_nonincremental_init (braced_init_obstack);
8309
8310 p = constructor_pending_elts;
8311 while (p)
8312 {
8313 if (field == p->purpose)
8314 return p->value;
8315 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8316 p = p->left;
8317 else
8318 p = p->right;
8319 }
8320 }
8321 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8322 {
8323 if (!vec_safe_is_empty (constructor_elements)
8324 && (constructor_elements->last ().index == field))
8325 return constructor_elements->last ().value;
8326 }
8327 return 0;
8328 }
8329
8330 /* "Output" the next constructor element.
8331 At top level, really output it to assembler code now.
8332 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8333 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8334 TYPE is the data type that the containing data type wants here.
8335 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8336 If VALUE is a string constant, STRICT_STRING is true if it is
8337 unparenthesized or we should not warn here for it being parenthesized.
8338 For other types of VALUE, STRICT_STRING is not used.
8339
8340 PENDING if non-nil means output pending elements that belong
8341 right after this element. (PENDING is normally 1;
8342 it is 0 while outputting pending elements, to avoid recursion.)
8343
8344 IMPLICIT is true if value comes from pop_init_level (1),
8345 the new initializer has been merged with the existing one
8346 and thus no warnings should be emitted about overriding an
8347 existing initializer. */
8348
8349 static void
8350 output_init_element (location_t loc, tree value, tree origtype,
8351 bool strict_string, tree type, tree field, int pending,
8352 bool implicit, struct obstack * braced_init_obstack)
8353 {
8354 tree semantic_type = NULL_TREE;
8355 bool maybe_const = true;
8356 bool npc;
8357
8358 if (type == error_mark_node || value == error_mark_node)
8359 {
8360 constructor_erroneous = 1;
8361 return;
8362 }
8363 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8364 && (TREE_CODE (value) == STRING_CST
8365 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8366 && !(TREE_CODE (value) == STRING_CST
8367 && TREE_CODE (type) == ARRAY_TYPE
8368 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8369 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8370 TYPE_MAIN_VARIANT (type)))
8371 value = array_to_pointer_conversion (input_location, value);
8372
8373 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8374 && require_constant_value && pending)
8375 {
8376 /* As an extension, allow initializing objects with static storage
8377 duration with compound literals (which are then treated just as
8378 the brace enclosed list they contain). */
8379 if (flag_isoc99)
8380 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8381 "constant");
8382 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8383 value = DECL_INITIAL (decl);
8384 }
8385
8386 npc = null_pointer_constant_p (value);
8387 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8388 {
8389 semantic_type = TREE_TYPE (value);
8390 value = TREE_OPERAND (value, 0);
8391 }
8392 value = c_fully_fold (value, require_constant_value, &maybe_const);
8393
8394 if (value == error_mark_node)
8395 constructor_erroneous = 1;
8396 else if (!TREE_CONSTANT (value))
8397 constructor_constant = 0;
8398 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8399 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8400 || TREE_CODE (constructor_type) == UNION_TYPE)
8401 && DECL_C_BIT_FIELD (field)
8402 && TREE_CODE (value) != INTEGER_CST))
8403 constructor_simple = 0;
8404 if (!maybe_const)
8405 constructor_nonconst = 1;
8406
8407 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8408 {
8409 if (require_constant_value)
8410 {
8411 error_init (loc, "initializer element is not constant");
8412 value = error_mark_node;
8413 }
8414 else if (require_constant_elements)
8415 pedwarn (loc, OPT_Wpedantic,
8416 "initializer element is not computable at load time");
8417 }
8418 else if (!maybe_const
8419 && (require_constant_value || require_constant_elements))
8420 pedwarn_init (loc, OPT_Wpedantic,
8421 "initializer element is not a constant expression");
8422
8423 /* Issue -Wc++-compat warnings about initializing a bitfield with
8424 enum type. */
8425 if (warn_cxx_compat
8426 && field != NULL_TREE
8427 && TREE_CODE (field) == FIELD_DECL
8428 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8429 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8430 != TYPE_MAIN_VARIANT (type))
8431 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8432 {
8433 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8434 if (checktype != error_mark_node
8435 && (TYPE_MAIN_VARIANT (checktype)
8436 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8437 warning_init (loc, OPT_Wc___compat,
8438 "enum conversion in initialization is invalid in C++");
8439 }
8440
8441 /* If this field is empty (and not at the end of structure),
8442 don't do anything other than checking the initializer. */
8443 if (field
8444 && (TREE_TYPE (field) == error_mark_node
8445 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8446 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8447 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8448 || DECL_CHAIN (field)))))
8449 return;
8450
8451 if (semantic_type)
8452 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8453 value = digest_init (loc, type, value, origtype, npc, strict_string,
8454 require_constant_value);
8455 if (value == error_mark_node)
8456 {
8457 constructor_erroneous = 1;
8458 return;
8459 }
8460 if (require_constant_value || require_constant_elements)
8461 constant_expression_warning (value);
8462
8463 /* If this element doesn't come next in sequence,
8464 put it on constructor_pending_elts. */
8465 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8466 && (!constructor_incremental
8467 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8468 {
8469 if (constructor_incremental
8470 && tree_int_cst_lt (field, constructor_unfilled_index))
8471 set_nonincremental_init (braced_init_obstack);
8472
8473 add_pending_init (loc, field, value, origtype, implicit,
8474 braced_init_obstack);
8475 return;
8476 }
8477 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8478 && (!constructor_incremental
8479 || field != constructor_unfilled_fields))
8480 {
8481 /* We do this for records but not for unions. In a union,
8482 no matter which field is specified, it can be initialized
8483 right away since it starts at the beginning of the union. */
8484 if (constructor_incremental)
8485 {
8486 if (!constructor_unfilled_fields)
8487 set_nonincremental_init (braced_init_obstack);
8488 else
8489 {
8490 tree bitpos, unfillpos;
8491
8492 bitpos = bit_position (field);
8493 unfillpos = bit_position (constructor_unfilled_fields);
8494
8495 if (tree_int_cst_lt (bitpos, unfillpos))
8496 set_nonincremental_init (braced_init_obstack);
8497 }
8498 }
8499
8500 add_pending_init (loc, field, value, origtype, implicit,
8501 braced_init_obstack);
8502 return;
8503 }
8504 else if (TREE_CODE (constructor_type) == UNION_TYPE
8505 && !vec_safe_is_empty (constructor_elements))
8506 {
8507 if (!implicit)
8508 {
8509 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8510 warning_init (loc, 0,
8511 "initialized field with side-effects overwritten");
8512 else if (warn_override_init)
8513 warning_init (loc, OPT_Woverride_init,
8514 "initialized field overwritten");
8515 }
8516
8517 /* We can have just one union field set. */
8518 constructor_elements = NULL;
8519 }
8520
8521 /* Otherwise, output this element either to
8522 constructor_elements or to the assembler file. */
8523
8524 constructor_elt celt = {field, value};
8525 vec_safe_push (constructor_elements, celt);
8526
8527 /* Advance the variable that indicates sequential elements output. */
8528 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8529 constructor_unfilled_index
8530 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8531 bitsize_one_node);
8532 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8533 {
8534 constructor_unfilled_fields
8535 = DECL_CHAIN (constructor_unfilled_fields);
8536
8537 /* Skip any nameless bit fields. */
8538 while (constructor_unfilled_fields != 0
8539 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8540 && DECL_NAME (constructor_unfilled_fields) == 0)
8541 constructor_unfilled_fields =
8542 DECL_CHAIN (constructor_unfilled_fields);
8543 }
8544 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8545 constructor_unfilled_fields = 0;
8546
8547 /* Now output any pending elements which have become next. */
8548 if (pending)
8549 output_pending_init_elements (0, braced_init_obstack);
8550 }
8551
8552 /* Output any pending elements which have become next.
8553 As we output elements, constructor_unfilled_{fields,index}
8554 advances, which may cause other elements to become next;
8555 if so, they too are output.
8556
8557 If ALL is 0, we return when there are
8558 no more pending elements to output now.
8559
8560 If ALL is 1, we output space as necessary so that
8561 we can output all the pending elements. */
8562 static void
8563 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8564 {
8565 struct init_node *elt = constructor_pending_elts;
8566 tree next;
8567
8568 retry:
8569
8570 /* Look through the whole pending tree.
8571 If we find an element that should be output now,
8572 output it. Otherwise, set NEXT to the element
8573 that comes first among those still pending. */
8574
8575 next = 0;
8576 while (elt)
8577 {
8578 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8579 {
8580 if (tree_int_cst_equal (elt->purpose,
8581 constructor_unfilled_index))
8582 output_init_element (input_location, elt->value, elt->origtype,
8583 true, TREE_TYPE (constructor_type),
8584 constructor_unfilled_index, 0, false,
8585 braced_init_obstack);
8586 else if (tree_int_cst_lt (constructor_unfilled_index,
8587 elt->purpose))
8588 {
8589 /* Advance to the next smaller node. */
8590 if (elt->left)
8591 elt = elt->left;
8592 else
8593 {
8594 /* We have reached the smallest node bigger than the
8595 current unfilled index. Fill the space first. */
8596 next = elt->purpose;
8597 break;
8598 }
8599 }
8600 else
8601 {
8602 /* Advance to the next bigger node. */
8603 if (elt->right)
8604 elt = elt->right;
8605 else
8606 {
8607 /* We have reached the biggest node in a subtree. Find
8608 the parent of it, which is the next bigger node. */
8609 while (elt->parent && elt->parent->right == elt)
8610 elt = elt->parent;
8611 elt = elt->parent;
8612 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8613 elt->purpose))
8614 {
8615 next = elt->purpose;
8616 break;
8617 }
8618 }
8619 }
8620 }
8621 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8622 || TREE_CODE (constructor_type) == UNION_TYPE)
8623 {
8624 tree ctor_unfilled_bitpos, elt_bitpos;
8625
8626 /* If the current record is complete we are done. */
8627 if (constructor_unfilled_fields == 0)
8628 break;
8629
8630 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8631 elt_bitpos = bit_position (elt->purpose);
8632 /* We can't compare fields here because there might be empty
8633 fields in between. */
8634 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8635 {
8636 constructor_unfilled_fields = elt->purpose;
8637 output_init_element (input_location, elt->value, elt->origtype,
8638 true, TREE_TYPE (elt->purpose),
8639 elt->purpose, 0, false,
8640 braced_init_obstack);
8641 }
8642 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8643 {
8644 /* Advance to the next smaller node. */
8645 if (elt->left)
8646 elt = elt->left;
8647 else
8648 {
8649 /* We have reached the smallest node bigger than the
8650 current unfilled field. Fill the space first. */
8651 next = elt->purpose;
8652 break;
8653 }
8654 }
8655 else
8656 {
8657 /* Advance to the next bigger node. */
8658 if (elt->right)
8659 elt = elt->right;
8660 else
8661 {
8662 /* We have reached the biggest node in a subtree. Find
8663 the parent of it, which is the next bigger node. */
8664 while (elt->parent && elt->parent->right == elt)
8665 elt = elt->parent;
8666 elt = elt->parent;
8667 if (elt
8668 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8669 bit_position (elt->purpose))))
8670 {
8671 next = elt->purpose;
8672 break;
8673 }
8674 }
8675 }
8676 }
8677 }
8678
8679 /* Ordinarily return, but not if we want to output all
8680 and there are elements left. */
8681 if (!(all && next != 0))
8682 return;
8683
8684 /* If it's not incremental, just skip over the gap, so that after
8685 jumping to retry we will output the next successive element. */
8686 if (TREE_CODE (constructor_type) == RECORD_TYPE
8687 || TREE_CODE (constructor_type) == UNION_TYPE)
8688 constructor_unfilled_fields = next;
8689 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8690 constructor_unfilled_index = next;
8691
8692 /* ELT now points to the node in the pending tree with the next
8693 initializer to output. */
8694 goto retry;
8695 }
8696 \f
8697 /* Add one non-braced element to the current constructor level.
8698 This adjusts the current position within the constructor's type.
8699 This may also start or terminate implicit levels
8700 to handle a partly-braced initializer.
8701
8702 Once this has found the correct level for the new element,
8703 it calls output_init_element.
8704
8705 IMPLICIT is true if value comes from pop_init_level (1),
8706 the new initializer has been merged with the existing one
8707 and thus no warnings should be emitted about overriding an
8708 existing initializer. */
8709
8710 void
8711 process_init_element (location_t loc, struct c_expr value, bool implicit,
8712 struct obstack * braced_init_obstack)
8713 {
8714 tree orig_value = value.value;
8715 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8716 bool strict_string = value.original_code == STRING_CST;
8717 bool was_designated = designator_depth != 0;
8718
8719 designator_depth = 0;
8720 designator_erroneous = 0;
8721
8722 if (!implicit && value.value && !integer_zerop (value.value))
8723 constructor_zeroinit = 0;
8724
8725 /* Handle superfluous braces around string cst as in
8726 char x[] = {"foo"}; */
8727 if (string_flag
8728 && constructor_type
8729 && !was_designated
8730 && TREE_CODE (constructor_type) == ARRAY_TYPE
8731 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8732 && integer_zerop (constructor_unfilled_index))
8733 {
8734 if (constructor_stack->replacement_value.value)
8735 error_init (loc, "excess elements in char array initializer");
8736 constructor_stack->replacement_value = value;
8737 return;
8738 }
8739
8740 if (constructor_stack->replacement_value.value != 0)
8741 {
8742 error_init (loc, "excess elements in struct initializer");
8743 return;
8744 }
8745
8746 /* Ignore elements of a brace group if it is entirely superfluous
8747 and has already been diagnosed. */
8748 if (constructor_type == 0)
8749 return;
8750
8751 if (!implicit && warn_designated_init && !was_designated
8752 && TREE_CODE (constructor_type) == RECORD_TYPE
8753 && lookup_attribute ("designated_init",
8754 TYPE_ATTRIBUTES (constructor_type)))
8755 warning_init (loc,
8756 OPT_Wdesignated_init,
8757 "positional initialization of field "
8758 "in %<struct%> declared with %<designated_init%> attribute");
8759
8760 /* If we've exhausted any levels that didn't have braces,
8761 pop them now. */
8762 while (constructor_stack->implicit)
8763 {
8764 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8765 || TREE_CODE (constructor_type) == UNION_TYPE)
8766 && constructor_fields == 0)
8767 process_init_element (loc,
8768 pop_init_level (loc, 1, braced_init_obstack),
8769 true, braced_init_obstack);
8770 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8771 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8772 && constructor_max_index
8773 && tree_int_cst_lt (constructor_max_index,
8774 constructor_index))
8775 process_init_element (loc,
8776 pop_init_level (loc, 1, braced_init_obstack),
8777 true, braced_init_obstack);
8778 else
8779 break;
8780 }
8781
8782 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8783 if (constructor_range_stack)
8784 {
8785 /* If value is a compound literal and we'll be just using its
8786 content, don't put it into a SAVE_EXPR. */
8787 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8788 || !require_constant_value
8789 || flag_isoc99)
8790 {
8791 tree semantic_type = NULL_TREE;
8792 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8793 {
8794 semantic_type = TREE_TYPE (value.value);
8795 value.value = TREE_OPERAND (value.value, 0);
8796 }
8797 value.value = c_save_expr (value.value);
8798 if (semantic_type)
8799 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8800 value.value);
8801 }
8802 }
8803
8804 while (1)
8805 {
8806 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8807 {
8808 tree fieldtype;
8809 enum tree_code fieldcode;
8810
8811 if (constructor_fields == 0)
8812 {
8813 pedwarn_init (loc, 0, "excess elements in struct initializer");
8814 break;
8815 }
8816
8817 fieldtype = TREE_TYPE (constructor_fields);
8818 if (fieldtype != error_mark_node)
8819 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8820 fieldcode = TREE_CODE (fieldtype);
8821
8822 /* Error for non-static initialization of a flexible array member. */
8823 if (fieldcode == ARRAY_TYPE
8824 && !require_constant_value
8825 && TYPE_SIZE (fieldtype) == NULL_TREE
8826 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8827 {
8828 error_init (loc, "non-static initialization of a flexible "
8829 "array member");
8830 break;
8831 }
8832
8833 /* Error for initialization of a flexible array member with
8834 a string constant if the structure is in an array. E.g.:
8835 struct S { int x; char y[]; };
8836 struct S s[] = { { 1, "foo" } };
8837 is invalid. */
8838 if (string_flag
8839 && fieldcode == ARRAY_TYPE
8840 && constructor_depth > 1
8841 && TYPE_SIZE (fieldtype) == NULL_TREE
8842 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8843 {
8844 bool in_array_p = false;
8845 for (struct constructor_stack *p = constructor_stack;
8846 p && p->type; p = p->next)
8847 if (TREE_CODE (p->type) == ARRAY_TYPE)
8848 {
8849 in_array_p = true;
8850 break;
8851 }
8852 if (in_array_p)
8853 {
8854 error_init (loc, "initialization of flexible array "
8855 "member in a nested context");
8856 break;
8857 }
8858 }
8859
8860 /* Accept a string constant to initialize a subarray. */
8861 if (value.value != 0
8862 && fieldcode == ARRAY_TYPE
8863 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8864 && string_flag)
8865 value.value = orig_value;
8866 /* Otherwise, if we have come to a subaggregate,
8867 and we don't have an element of its type, push into it. */
8868 else if (value.value != 0
8869 && value.value != error_mark_node
8870 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8871 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8872 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8873 {
8874 push_init_level (loc, 1, braced_init_obstack);
8875 continue;
8876 }
8877
8878 if (value.value)
8879 {
8880 push_member_name (constructor_fields);
8881 output_init_element (loc, value.value, value.original_type,
8882 strict_string, fieldtype,
8883 constructor_fields, 1, implicit,
8884 braced_init_obstack);
8885 RESTORE_SPELLING_DEPTH (constructor_depth);
8886 }
8887 else
8888 /* Do the bookkeeping for an element that was
8889 directly output as a constructor. */
8890 {
8891 /* For a record, keep track of end position of last field. */
8892 if (DECL_SIZE (constructor_fields))
8893 constructor_bit_index
8894 = size_binop_loc (input_location, PLUS_EXPR,
8895 bit_position (constructor_fields),
8896 DECL_SIZE (constructor_fields));
8897
8898 /* If the current field was the first one not yet written out,
8899 it isn't now, so update. */
8900 if (constructor_unfilled_fields == constructor_fields)
8901 {
8902 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8903 /* Skip any nameless bit fields. */
8904 while (constructor_unfilled_fields != 0
8905 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8906 && DECL_NAME (constructor_unfilled_fields) == 0)
8907 constructor_unfilled_fields =
8908 DECL_CHAIN (constructor_unfilled_fields);
8909 }
8910 }
8911
8912 constructor_fields = DECL_CHAIN (constructor_fields);
8913 /* Skip any nameless bit fields at the beginning. */
8914 while (constructor_fields != 0
8915 && DECL_C_BIT_FIELD (constructor_fields)
8916 && DECL_NAME (constructor_fields) == 0)
8917 constructor_fields = DECL_CHAIN (constructor_fields);
8918 }
8919 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8920 {
8921 tree fieldtype;
8922 enum tree_code fieldcode;
8923
8924 if (constructor_fields == 0)
8925 {
8926 pedwarn_init (loc, 0,
8927 "excess elements in union initializer");
8928 break;
8929 }
8930
8931 fieldtype = TREE_TYPE (constructor_fields);
8932 if (fieldtype != error_mark_node)
8933 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8934 fieldcode = TREE_CODE (fieldtype);
8935
8936 /* Warn that traditional C rejects initialization of unions.
8937 We skip the warning if the value is zero. This is done
8938 under the assumption that the zero initializer in user
8939 code appears conditioned on e.g. __STDC__ to avoid
8940 "missing initializer" warnings and relies on default
8941 initialization to zero in the traditional C case.
8942 We also skip the warning if the initializer is designated,
8943 again on the assumption that this must be conditional on
8944 __STDC__ anyway (and we've already complained about the
8945 member-designator already). */
8946 if (!in_system_header_at (input_location) && !constructor_designated
8947 && !(value.value && (integer_zerop (value.value)
8948 || real_zerop (value.value))))
8949 warning (OPT_Wtraditional, "traditional C rejects initialization "
8950 "of unions");
8951
8952 /* Accept a string constant to initialize a subarray. */
8953 if (value.value != 0
8954 && fieldcode == ARRAY_TYPE
8955 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8956 && string_flag)
8957 value.value = orig_value;
8958 /* Otherwise, if we have come to a subaggregate,
8959 and we don't have an element of its type, push into it. */
8960 else if (value.value != 0
8961 && value.value != error_mark_node
8962 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8963 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8964 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8965 {
8966 push_init_level (loc, 1, braced_init_obstack);
8967 continue;
8968 }
8969
8970 if (value.value)
8971 {
8972 push_member_name (constructor_fields);
8973 output_init_element (loc, value.value, value.original_type,
8974 strict_string, fieldtype,
8975 constructor_fields, 1, implicit,
8976 braced_init_obstack);
8977 RESTORE_SPELLING_DEPTH (constructor_depth);
8978 }
8979 else
8980 /* Do the bookkeeping for an element that was
8981 directly output as a constructor. */
8982 {
8983 constructor_bit_index = DECL_SIZE (constructor_fields);
8984 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8985 }
8986
8987 constructor_fields = 0;
8988 }
8989 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8990 {
8991 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8992 enum tree_code eltcode = TREE_CODE (elttype);
8993
8994 /* Accept a string constant to initialize a subarray. */
8995 if (value.value != 0
8996 && eltcode == ARRAY_TYPE
8997 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8998 && string_flag)
8999 value.value = orig_value;
9000 /* Otherwise, if we have come to a subaggregate,
9001 and we don't have an element of its type, push into it. */
9002 else if (value.value != 0
9003 && value.value != error_mark_node
9004 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9005 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9006 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9007 {
9008 push_init_level (loc, 1, braced_init_obstack);
9009 continue;
9010 }
9011
9012 if (constructor_max_index != 0
9013 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9014 || integer_all_onesp (constructor_max_index)))
9015 {
9016 pedwarn_init (loc, 0,
9017 "excess elements in array initializer");
9018 break;
9019 }
9020
9021 /* Now output the actual element. */
9022 if (value.value)
9023 {
9024 push_array_bounds (tree_to_uhwi (constructor_index));
9025 output_init_element (loc, value.value, value.original_type,
9026 strict_string, elttype,
9027 constructor_index, 1, implicit,
9028 braced_init_obstack);
9029 RESTORE_SPELLING_DEPTH (constructor_depth);
9030 }
9031
9032 constructor_index
9033 = size_binop_loc (input_location, PLUS_EXPR,
9034 constructor_index, bitsize_one_node);
9035
9036 if (!value.value)
9037 /* If we are doing the bookkeeping for an element that was
9038 directly output as a constructor, we must update
9039 constructor_unfilled_index. */
9040 constructor_unfilled_index = constructor_index;
9041 }
9042 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
9043 {
9044 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9045
9046 /* Do a basic check of initializer size. Note that vectors
9047 always have a fixed size derived from their type. */
9048 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9049 {
9050 pedwarn_init (loc, 0,
9051 "excess elements in vector initializer");
9052 break;
9053 }
9054
9055 /* Now output the actual element. */
9056 if (value.value)
9057 {
9058 if (TREE_CODE (value.value) == VECTOR_CST)
9059 elttype = TYPE_MAIN_VARIANT (constructor_type);
9060 output_init_element (loc, value.value, value.original_type,
9061 strict_string, elttype,
9062 constructor_index, 1, implicit,
9063 braced_init_obstack);
9064 }
9065
9066 constructor_index
9067 = size_binop_loc (input_location,
9068 PLUS_EXPR, constructor_index, bitsize_one_node);
9069
9070 if (!value.value)
9071 /* If we are doing the bookkeeping for an element that was
9072 directly output as a constructor, we must update
9073 constructor_unfilled_index. */
9074 constructor_unfilled_index = constructor_index;
9075 }
9076
9077 /* Handle the sole element allowed in a braced initializer
9078 for a scalar variable. */
9079 else if (constructor_type != error_mark_node
9080 && constructor_fields == 0)
9081 {
9082 pedwarn_init (loc, 0,
9083 "excess elements in scalar initializer");
9084 break;
9085 }
9086 else
9087 {
9088 if (value.value)
9089 output_init_element (loc, value.value, value.original_type,
9090 strict_string, constructor_type,
9091 NULL_TREE, 1, implicit,
9092 braced_init_obstack);
9093 constructor_fields = 0;
9094 }
9095
9096 /* Handle range initializers either at this level or anywhere higher
9097 in the designator stack. */
9098 if (constructor_range_stack)
9099 {
9100 struct constructor_range_stack *p, *range_stack;
9101 int finish = 0;
9102
9103 range_stack = constructor_range_stack;
9104 constructor_range_stack = 0;
9105 while (constructor_stack != range_stack->stack)
9106 {
9107 gcc_assert (constructor_stack->implicit);
9108 process_init_element (loc,
9109 pop_init_level (loc, 1,
9110 braced_init_obstack),
9111 true, braced_init_obstack);
9112 }
9113 for (p = range_stack;
9114 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9115 p = p->prev)
9116 {
9117 gcc_assert (constructor_stack->implicit);
9118 process_init_element (loc,
9119 pop_init_level (loc, 1,
9120 braced_init_obstack),
9121 true, braced_init_obstack);
9122 }
9123
9124 p->index = size_binop_loc (input_location,
9125 PLUS_EXPR, p->index, bitsize_one_node);
9126 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9127 finish = 1;
9128
9129 while (1)
9130 {
9131 constructor_index = p->index;
9132 constructor_fields = p->fields;
9133 if (finish && p->range_end && p->index == p->range_start)
9134 {
9135 finish = 0;
9136 p->prev = 0;
9137 }
9138 p = p->next;
9139 if (!p)
9140 break;
9141 push_init_level (loc, 2, braced_init_obstack);
9142 p->stack = constructor_stack;
9143 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9144 p->index = p->range_start;
9145 }
9146
9147 if (!finish)
9148 constructor_range_stack = range_stack;
9149 continue;
9150 }
9151
9152 break;
9153 }
9154
9155 constructor_range_stack = 0;
9156 }
9157 \f
9158 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9159 (guaranteed to be 'volatile' or null) and ARGS (represented using
9160 an ASM_EXPR node). */
9161 tree
9162 build_asm_stmt (tree cv_qualifier, tree args)
9163 {
9164 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9165 ASM_VOLATILE_P (args) = 1;
9166 return add_stmt (args);
9167 }
9168
9169 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9170 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9171 SIMPLE indicates whether there was anything at all after the
9172 string in the asm expression -- asm("blah") and asm("blah" : )
9173 are subtly different. We use a ASM_EXPR node to represent this. */
9174 tree
9175 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9176 tree clobbers, tree labels, bool simple)
9177 {
9178 tree tail;
9179 tree args;
9180 int i;
9181 const char *constraint;
9182 const char **oconstraints;
9183 bool allows_mem, allows_reg, is_inout;
9184 int ninputs, noutputs;
9185
9186 ninputs = list_length (inputs);
9187 noutputs = list_length (outputs);
9188 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9189
9190 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9191
9192 /* Remove output conversions that change the type but not the mode. */
9193 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9194 {
9195 tree output = TREE_VALUE (tail);
9196
9197 output = c_fully_fold (output, false, NULL);
9198
9199 /* ??? Really, this should not be here. Users should be using a
9200 proper lvalue, dammit. But there's a long history of using casts
9201 in the output operands. In cases like longlong.h, this becomes a
9202 primitive form of typechecking -- if the cast can be removed, then
9203 the output operand had a type of the proper width; otherwise we'll
9204 get an error. Gross, but ... */
9205 STRIP_NOPS (output);
9206
9207 if (!lvalue_or_else (loc, output, lv_asm))
9208 output = error_mark_node;
9209
9210 if (output != error_mark_node
9211 && (TREE_READONLY (output)
9212 || TYPE_READONLY (TREE_TYPE (output))
9213 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9214 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9215 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9216 readonly_error (loc, output, lv_asm);
9217
9218 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9219 oconstraints[i] = constraint;
9220
9221 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9222 &allows_mem, &allows_reg, &is_inout))
9223 {
9224 /* If the operand is going to end up in memory,
9225 mark it addressable. */
9226 if (!allows_reg && !c_mark_addressable (output))
9227 output = error_mark_node;
9228 if (!(!allows_reg && allows_mem)
9229 && output != error_mark_node
9230 && VOID_TYPE_P (TREE_TYPE (output)))
9231 {
9232 error_at (loc, "invalid use of void expression");
9233 output = error_mark_node;
9234 }
9235 }
9236 else
9237 output = error_mark_node;
9238
9239 TREE_VALUE (tail) = output;
9240 }
9241
9242 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9243 {
9244 tree input;
9245
9246 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9247 input = TREE_VALUE (tail);
9248
9249 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9250 oconstraints, &allows_mem, &allows_reg))
9251 {
9252 /* If the operand is going to end up in memory,
9253 mark it addressable. */
9254 if (!allows_reg && allows_mem)
9255 {
9256 input = c_fully_fold (input, false, NULL);
9257
9258 /* Strip the nops as we allow this case. FIXME, this really
9259 should be rejected or made deprecated. */
9260 STRIP_NOPS (input);
9261 if (!c_mark_addressable (input))
9262 input = error_mark_node;
9263 }
9264 else
9265 {
9266 struct c_expr expr;
9267 memset (&expr, 0, sizeof (expr));
9268 expr.value = input;
9269 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9270 input = c_fully_fold (expr.value, false, NULL);
9271
9272 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9273 {
9274 error_at (loc, "invalid use of void expression");
9275 input = error_mark_node;
9276 }
9277 }
9278 }
9279 else
9280 input = error_mark_node;
9281
9282 TREE_VALUE (tail) = input;
9283 }
9284
9285 /* ASMs with labels cannot have outputs. This should have been
9286 enforced by the parser. */
9287 gcc_assert (outputs == NULL || labels == NULL);
9288
9289 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9290
9291 /* asm statements without outputs, including simple ones, are treated
9292 as volatile. */
9293 ASM_INPUT_P (args) = simple;
9294 ASM_VOLATILE_P (args) = (noutputs == 0);
9295
9296 return args;
9297 }
9298 \f
9299 /* Generate a goto statement to LABEL. LOC is the location of the
9300 GOTO. */
9301
9302 tree
9303 c_finish_goto_label (location_t loc, tree label)
9304 {
9305 tree decl = lookup_label_for_goto (loc, label);
9306 if (!decl)
9307 return NULL_TREE;
9308 TREE_USED (decl) = 1;
9309 {
9310 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9311 SET_EXPR_LOCATION (t, loc);
9312 return add_stmt (t);
9313 }
9314 }
9315
9316 /* Generate a computed goto statement to EXPR. LOC is the location of
9317 the GOTO. */
9318
9319 tree
9320 c_finish_goto_ptr (location_t loc, tree expr)
9321 {
9322 tree t;
9323 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9324 expr = c_fully_fold (expr, false, NULL);
9325 expr = convert (ptr_type_node, expr);
9326 t = build1 (GOTO_EXPR, void_type_node, expr);
9327 SET_EXPR_LOCATION (t, loc);
9328 return add_stmt (t);
9329 }
9330
9331 /* Generate a C `return' statement. RETVAL is the expression for what
9332 to return, or a null pointer for `return;' with no value. LOC is
9333 the location of the return statement, or the location of the expression,
9334 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9335 is the original type of RETVAL. */
9336
9337 tree
9338 c_finish_return (location_t loc, tree retval, tree origtype)
9339 {
9340 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9341 bool no_warning = false;
9342 bool npc = false;
9343 size_t rank = 0;
9344
9345 if (TREE_THIS_VOLATILE (current_function_decl))
9346 warning_at (loc, 0,
9347 "function declared %<noreturn%> has a %<return%> statement");
9348
9349 if (flag_cilkplus && contains_array_notation_expr (retval))
9350 {
9351 /* Array notations are allowed in a return statement if it is inside a
9352 built-in array notation reduction function. */
9353 if (!find_rank (loc, retval, retval, false, &rank))
9354 return error_mark_node;
9355 if (rank >= 1)
9356 {
9357 error_at (loc, "array notation expression cannot be used as a "
9358 "return value");
9359 return error_mark_node;
9360 }
9361 }
9362 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9363 {
9364 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9365 "allowed");
9366 return error_mark_node;
9367 }
9368 if (retval)
9369 {
9370 tree semantic_type = NULL_TREE;
9371 npc = null_pointer_constant_p (retval);
9372 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9373 {
9374 semantic_type = TREE_TYPE (retval);
9375 retval = TREE_OPERAND (retval, 0);
9376 }
9377 retval = c_fully_fold (retval, false, NULL);
9378 if (semantic_type)
9379 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9380 }
9381
9382 if (!retval)
9383 {
9384 current_function_returns_null = 1;
9385 if ((warn_return_type || flag_isoc99)
9386 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9387 {
9388 if (flag_isoc99)
9389 pedwarn (loc, 0, "%<return%> with no value, in "
9390 "function returning non-void");
9391 else
9392 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9393 "in function returning non-void");
9394 no_warning = true;
9395 }
9396 }
9397 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9398 {
9399 current_function_returns_null = 1;
9400 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9401 pedwarn (loc, 0,
9402 "%<return%> with a value, in function returning void");
9403 else
9404 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9405 "%<return%> with expression, in function returning void");
9406 }
9407 else
9408 {
9409 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9410 retval, origtype, ic_return,
9411 npc, NULL_TREE, NULL_TREE, 0);
9412 tree res = DECL_RESULT (current_function_decl);
9413 tree inner;
9414 bool save;
9415
9416 current_function_returns_value = 1;
9417 if (t == error_mark_node)
9418 return NULL_TREE;
9419
9420 save = in_late_binary_op;
9421 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9422 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9423 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9424 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9425 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9426 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9427 in_late_binary_op = true;
9428 inner = t = convert (TREE_TYPE (res), t);
9429 in_late_binary_op = save;
9430
9431 /* Strip any conversions, additions, and subtractions, and see if
9432 we are returning the address of a local variable. Warn if so. */
9433 while (1)
9434 {
9435 switch (TREE_CODE (inner))
9436 {
9437 CASE_CONVERT:
9438 case NON_LVALUE_EXPR:
9439 case PLUS_EXPR:
9440 case POINTER_PLUS_EXPR:
9441 inner = TREE_OPERAND (inner, 0);
9442 continue;
9443
9444 case MINUS_EXPR:
9445 /* If the second operand of the MINUS_EXPR has a pointer
9446 type (or is converted from it), this may be valid, so
9447 don't give a warning. */
9448 {
9449 tree op1 = TREE_OPERAND (inner, 1);
9450
9451 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9452 && (CONVERT_EXPR_P (op1)
9453 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9454 op1 = TREE_OPERAND (op1, 0);
9455
9456 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9457 break;
9458
9459 inner = TREE_OPERAND (inner, 0);
9460 continue;
9461 }
9462
9463 case ADDR_EXPR:
9464 inner = TREE_OPERAND (inner, 0);
9465
9466 while (REFERENCE_CLASS_P (inner)
9467 && TREE_CODE (inner) != INDIRECT_REF)
9468 inner = TREE_OPERAND (inner, 0);
9469
9470 if (DECL_P (inner)
9471 && !DECL_EXTERNAL (inner)
9472 && !TREE_STATIC (inner)
9473 && DECL_CONTEXT (inner) == current_function_decl)
9474 {
9475 if (TREE_CODE (inner) == LABEL_DECL)
9476 warning_at (loc, OPT_Wreturn_local_addr,
9477 "function returns address of label");
9478 else
9479 {
9480 warning_at (loc, OPT_Wreturn_local_addr,
9481 "function returns address of local variable");
9482 tree zero = build_zero_cst (TREE_TYPE (res));
9483 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9484 }
9485 }
9486 break;
9487
9488 default:
9489 break;
9490 }
9491
9492 break;
9493 }
9494
9495 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9496 SET_EXPR_LOCATION (retval, loc);
9497
9498 if (warn_sequence_point)
9499 verify_sequence_points (retval);
9500 }
9501
9502 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9503 TREE_NO_WARNING (ret_stmt) |= no_warning;
9504 return add_stmt (ret_stmt);
9505 }
9506 \f
9507 struct c_switch {
9508 /* The SWITCH_EXPR being built. */
9509 tree switch_expr;
9510
9511 /* The original type of the testing expression, i.e. before the
9512 default conversion is applied. */
9513 tree orig_type;
9514
9515 /* A splay-tree mapping the low element of a case range to the high
9516 element, or NULL_TREE if there is no high element. Used to
9517 determine whether or not a new case label duplicates an old case
9518 label. We need a tree, rather than simply a hash table, because
9519 of the GNU case range extension. */
9520 splay_tree cases;
9521
9522 /* The bindings at the point of the switch. This is used for
9523 warnings crossing decls when branching to a case label. */
9524 struct c_spot_bindings *bindings;
9525
9526 /* The next node on the stack. */
9527 struct c_switch *next;
9528 };
9529
9530 /* A stack of the currently active switch statements. The innermost
9531 switch statement is on the top of the stack. There is no need to
9532 mark the stack for garbage collection because it is only active
9533 during the processing of the body of a function, and we never
9534 collect at that point. */
9535
9536 struct c_switch *c_switch_stack;
9537
9538 /* Start a C switch statement, testing expression EXP. Return the new
9539 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9540 SWITCH_COND_LOC is the location of the switch's condition.
9541 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9542
9543 tree
9544 c_start_case (location_t switch_loc,
9545 location_t switch_cond_loc,
9546 tree exp, bool explicit_cast_p)
9547 {
9548 tree orig_type = error_mark_node;
9549 struct c_switch *cs;
9550
9551 if (exp != error_mark_node)
9552 {
9553 orig_type = TREE_TYPE (exp);
9554
9555 if (!INTEGRAL_TYPE_P (orig_type))
9556 {
9557 if (orig_type != error_mark_node)
9558 {
9559 error_at (switch_cond_loc, "switch quantity not an integer");
9560 orig_type = error_mark_node;
9561 }
9562 exp = integer_zero_node;
9563 }
9564 else
9565 {
9566 tree type = TYPE_MAIN_VARIANT (orig_type);
9567 tree e = exp;
9568
9569 /* Warn if the condition has boolean value. */
9570 while (TREE_CODE (e) == COMPOUND_EXPR)
9571 e = TREE_OPERAND (e, 1);
9572
9573 if ((TREE_CODE (type) == BOOLEAN_TYPE
9574 || truth_value_p (TREE_CODE (e)))
9575 /* Explicit cast to int suppresses this warning. */
9576 && !(TREE_CODE (type) == INTEGER_TYPE
9577 && explicit_cast_p))
9578 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9579 "switch condition has boolean value");
9580
9581 if (!in_system_header_at (input_location)
9582 && (type == long_integer_type_node
9583 || type == long_unsigned_type_node))
9584 warning_at (switch_cond_loc,
9585 OPT_Wtraditional, "%<long%> switch expression not "
9586 "converted to %<int%> in ISO C");
9587
9588 exp = c_fully_fold (exp, false, NULL);
9589 exp = default_conversion (exp);
9590
9591 if (warn_sequence_point)
9592 verify_sequence_points (exp);
9593 }
9594 }
9595
9596 /* Add this new SWITCH_EXPR to the stack. */
9597 cs = XNEW (struct c_switch);
9598 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9599 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9600 cs->orig_type = orig_type;
9601 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9602 cs->bindings = c_get_switch_bindings ();
9603 cs->next = c_switch_stack;
9604 c_switch_stack = cs;
9605
9606 return add_stmt (cs->switch_expr);
9607 }
9608
9609 /* Process a case label at location LOC. */
9610
9611 tree
9612 do_case (location_t loc, tree low_value, tree high_value)
9613 {
9614 tree label = NULL_TREE;
9615
9616 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9617 {
9618 low_value = c_fully_fold (low_value, false, NULL);
9619 if (TREE_CODE (low_value) == INTEGER_CST)
9620 pedwarn (loc, OPT_Wpedantic,
9621 "case label is not an integer constant expression");
9622 }
9623
9624 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9625 {
9626 high_value = c_fully_fold (high_value, false, NULL);
9627 if (TREE_CODE (high_value) == INTEGER_CST)
9628 pedwarn (input_location, OPT_Wpedantic,
9629 "case label is not an integer constant expression");
9630 }
9631
9632 if (c_switch_stack == NULL)
9633 {
9634 if (low_value)
9635 error_at (loc, "case label not within a switch statement");
9636 else
9637 error_at (loc, "%<default%> label not within a switch statement");
9638 return NULL_TREE;
9639 }
9640
9641 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9642 EXPR_LOCATION (c_switch_stack->switch_expr),
9643 loc))
9644 return NULL_TREE;
9645
9646 label = c_add_case_label (loc, c_switch_stack->cases,
9647 SWITCH_COND (c_switch_stack->switch_expr),
9648 c_switch_stack->orig_type,
9649 low_value, high_value);
9650 if (label == error_mark_node)
9651 label = NULL_TREE;
9652 return label;
9653 }
9654
9655 /* Finish the switch statement. TYPE is the original type of the
9656 controlling expression of the switch, or NULL_TREE. */
9657
9658 void
9659 c_finish_case (tree body, tree type)
9660 {
9661 struct c_switch *cs = c_switch_stack;
9662 location_t switch_location;
9663
9664 SWITCH_BODY (cs->switch_expr) = body;
9665
9666 /* Emit warnings as needed. */
9667 switch_location = EXPR_LOCATION (cs->switch_expr);
9668 c_do_switch_warnings (cs->cases, switch_location,
9669 type ? type : TREE_TYPE (cs->switch_expr),
9670 SWITCH_COND (cs->switch_expr));
9671
9672 /* Pop the stack. */
9673 c_switch_stack = cs->next;
9674 splay_tree_delete (cs->cases);
9675 c_release_switch_bindings (cs->bindings);
9676 XDELETE (cs);
9677 }
9678 \f
9679 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9680 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9681 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9682 statement, and was not surrounded with parenthesis. */
9683
9684 void
9685 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9686 tree else_block, bool nested_if)
9687 {
9688 tree stmt;
9689
9690 /* If the condition has array notations, then the rank of the then_block and
9691 else_block must be either 0 or be equal to the rank of the condition. If
9692 the condition does not have array notations then break them up as it is
9693 broken up in a normal expression. */
9694 if (flag_cilkplus && contains_array_notation_expr (cond))
9695 {
9696 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9697 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9698 return;
9699 if (then_block
9700 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9701 return;
9702 if (else_block
9703 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9704 return;
9705 if (cond_rank != then_rank && then_rank != 0)
9706 {
9707 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9708 " and the then-block");
9709 return;
9710 }
9711 else if (cond_rank != else_rank && else_rank != 0)
9712 {
9713 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9714 " and the else-block");
9715 return;
9716 }
9717 }
9718 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9719 if (warn_parentheses && nested_if && else_block == NULL)
9720 {
9721 tree inner_if = then_block;
9722
9723 /* We know from the grammar productions that there is an IF nested
9724 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9725 it might not be exactly THEN_BLOCK, but should be the last
9726 non-container statement within. */
9727 while (1)
9728 switch (TREE_CODE (inner_if))
9729 {
9730 case COND_EXPR:
9731 goto found;
9732 case BIND_EXPR:
9733 inner_if = BIND_EXPR_BODY (inner_if);
9734 break;
9735 case STATEMENT_LIST:
9736 inner_if = expr_last (then_block);
9737 break;
9738 case TRY_FINALLY_EXPR:
9739 case TRY_CATCH_EXPR:
9740 inner_if = TREE_OPERAND (inner_if, 0);
9741 break;
9742 default:
9743 gcc_unreachable ();
9744 }
9745 found:
9746
9747 if (COND_EXPR_ELSE (inner_if))
9748 warning_at (if_locus, OPT_Wparentheses,
9749 "suggest explicit braces to avoid ambiguous %<else%>");
9750 }
9751
9752 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9753 SET_EXPR_LOCATION (stmt, if_locus);
9754 add_stmt (stmt);
9755 }
9756
9757 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9758 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9759 is false for DO loops. INCR is the FOR increment expression. BODY is
9760 the statement controlled by the loop. BLAB is the break label. CLAB is
9761 the continue label. Everything is allowed to be NULL. */
9762
9763 void
9764 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9765 tree blab, tree clab, bool cond_is_first)
9766 {
9767 tree entry = NULL, exit = NULL, t;
9768
9769 /* In theory could forbid cilk spawn for loop increment expression,
9770 but it should work just fine. */
9771
9772 /* If the condition is zero don't generate a loop construct. */
9773 if (cond && integer_zerop (cond))
9774 {
9775 if (cond_is_first)
9776 {
9777 t = build_and_jump (&blab);
9778 SET_EXPR_LOCATION (t, start_locus);
9779 add_stmt (t);
9780 }
9781 }
9782 else
9783 {
9784 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9785
9786 /* If we have an exit condition, then we build an IF with gotos either
9787 out of the loop, or to the top of it. If there's no exit condition,
9788 then we just build a jump back to the top. */
9789 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9790
9791 if (cond && !integer_nonzerop (cond))
9792 {
9793 /* Canonicalize the loop condition to the end. This means
9794 generating a branch to the loop condition. Reuse the
9795 continue label, if possible. */
9796 if (cond_is_first)
9797 {
9798 if (incr || !clab)
9799 {
9800 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9801 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9802 }
9803 else
9804 t = build1 (GOTO_EXPR, void_type_node, clab);
9805 SET_EXPR_LOCATION (t, start_locus);
9806 add_stmt (t);
9807 }
9808
9809 t = build_and_jump (&blab);
9810 if (cond_is_first)
9811 exit = fold_build3_loc (start_locus,
9812 COND_EXPR, void_type_node, cond, exit, t);
9813 else
9814 exit = fold_build3_loc (input_location,
9815 COND_EXPR, void_type_node, cond, exit, t);
9816 }
9817
9818 add_stmt (top);
9819 }
9820
9821 if (body)
9822 add_stmt (body);
9823 if (clab)
9824 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9825 if (incr)
9826 add_stmt (incr);
9827 if (entry)
9828 add_stmt (entry);
9829 if (exit)
9830 add_stmt (exit);
9831 if (blab)
9832 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9833 }
9834
9835 tree
9836 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9837 {
9838 bool skip;
9839 tree label = *label_p;
9840
9841 /* In switch statements break is sometimes stylistically used after
9842 a return statement. This can lead to spurious warnings about
9843 control reaching the end of a non-void function when it is
9844 inlined. Note that we are calling block_may_fallthru with
9845 language specific tree nodes; this works because
9846 block_may_fallthru returns true when given something it does not
9847 understand. */
9848 skip = !block_may_fallthru (cur_stmt_list);
9849
9850 if (!label)
9851 {
9852 if (!skip)
9853 *label_p = label = create_artificial_label (loc);
9854 }
9855 else if (TREE_CODE (label) == LABEL_DECL)
9856 ;
9857 else switch (TREE_INT_CST_LOW (label))
9858 {
9859 case 0:
9860 if (is_break)
9861 error_at (loc, "break statement not within loop or switch");
9862 else
9863 error_at (loc, "continue statement not within a loop");
9864 return NULL_TREE;
9865
9866 case 1:
9867 gcc_assert (is_break);
9868 error_at (loc, "break statement used with OpenMP for loop");
9869 return NULL_TREE;
9870
9871 case 2:
9872 if (is_break)
9873 error ("break statement within %<#pragma simd%> loop body");
9874 else
9875 error ("continue statement within %<#pragma simd%> loop body");
9876 return NULL_TREE;
9877
9878 default:
9879 gcc_unreachable ();
9880 }
9881
9882 if (skip)
9883 return NULL_TREE;
9884
9885 if (!is_break)
9886 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9887
9888 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9889 }
9890
9891 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9892
9893 static void
9894 emit_side_effect_warnings (location_t loc, tree expr)
9895 {
9896 if (expr == error_mark_node)
9897 ;
9898 else if (!TREE_SIDE_EFFECTS (expr))
9899 {
9900 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9901 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9902 }
9903 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9904 {
9905 tree r = expr;
9906 location_t cloc = loc;
9907 while (TREE_CODE (r) == COMPOUND_EXPR)
9908 {
9909 if (EXPR_HAS_LOCATION (r))
9910 cloc = EXPR_LOCATION (r);
9911 r = TREE_OPERAND (r, 1);
9912 }
9913 if (!TREE_SIDE_EFFECTS (r)
9914 && !VOID_TYPE_P (TREE_TYPE (r))
9915 && !CONVERT_EXPR_P (r)
9916 && !TREE_NO_WARNING (r)
9917 && !TREE_NO_WARNING (expr))
9918 warning_at (cloc, OPT_Wunused_value,
9919 "right-hand operand of comma expression has no effect");
9920 }
9921 else
9922 warn_if_unused_value (expr, loc);
9923 }
9924
9925 /* Process an expression as if it were a complete statement. Emit
9926 diagnostics, but do not call ADD_STMT. LOC is the location of the
9927 statement. */
9928
9929 tree
9930 c_process_expr_stmt (location_t loc, tree expr)
9931 {
9932 tree exprv;
9933
9934 if (!expr)
9935 return NULL_TREE;
9936
9937 expr = c_fully_fold (expr, false, NULL);
9938
9939 if (warn_sequence_point)
9940 verify_sequence_points (expr);
9941
9942 if (TREE_TYPE (expr) != error_mark_node
9943 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9944 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9945 error_at (loc, "expression statement has incomplete type");
9946
9947 /* If we're not processing a statement expression, warn about unused values.
9948 Warnings for statement expressions will be emitted later, once we figure
9949 out which is the result. */
9950 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9951 && warn_unused_value)
9952 emit_side_effect_warnings (loc, expr);
9953
9954 exprv = expr;
9955 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9956 exprv = TREE_OPERAND (exprv, 1);
9957 while (CONVERT_EXPR_P (exprv))
9958 exprv = TREE_OPERAND (exprv, 0);
9959 if (DECL_P (exprv)
9960 || handled_component_p (exprv)
9961 || TREE_CODE (exprv) == ADDR_EXPR)
9962 mark_exp_read (exprv);
9963
9964 /* If the expression is not of a type to which we cannot assign a line
9965 number, wrap the thing in a no-op NOP_EXPR. */
9966 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9967 {
9968 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9969 SET_EXPR_LOCATION (expr, loc);
9970 }
9971
9972 return expr;
9973 }
9974
9975 /* Emit an expression as a statement. LOC is the location of the
9976 expression. */
9977
9978 tree
9979 c_finish_expr_stmt (location_t loc, tree expr)
9980 {
9981 if (expr)
9982 return add_stmt (c_process_expr_stmt (loc, expr));
9983 else
9984 return NULL;
9985 }
9986
9987 /* Do the opposite and emit a statement as an expression. To begin,
9988 create a new binding level and return it. */
9989
9990 tree
9991 c_begin_stmt_expr (void)
9992 {
9993 tree ret;
9994
9995 /* We must force a BLOCK for this level so that, if it is not expanded
9996 later, there is a way to turn off the entire subtree of blocks that
9997 are contained in it. */
9998 keep_next_level ();
9999 ret = c_begin_compound_stmt (true);
10000
10001 c_bindings_start_stmt_expr (c_switch_stack == NULL
10002 ? NULL
10003 : c_switch_stack->bindings);
10004
10005 /* Mark the current statement list as belonging to a statement list. */
10006 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10007
10008 return ret;
10009 }
10010
10011 /* LOC is the location of the compound statement to which this body
10012 belongs. */
10013
10014 tree
10015 c_finish_stmt_expr (location_t loc, tree body)
10016 {
10017 tree last, type, tmp, val;
10018 tree *last_p;
10019
10020 body = c_end_compound_stmt (loc, body, true);
10021
10022 c_bindings_end_stmt_expr (c_switch_stack == NULL
10023 ? NULL
10024 : c_switch_stack->bindings);
10025
10026 /* Locate the last statement in BODY. See c_end_compound_stmt
10027 about always returning a BIND_EXPR. */
10028 last_p = &BIND_EXPR_BODY (body);
10029 last = BIND_EXPR_BODY (body);
10030
10031 continue_searching:
10032 if (TREE_CODE (last) == STATEMENT_LIST)
10033 {
10034 tree_stmt_iterator i;
10035
10036 /* This can happen with degenerate cases like ({ }). No value. */
10037 if (!TREE_SIDE_EFFECTS (last))
10038 return body;
10039
10040 /* If we're supposed to generate side effects warnings, process
10041 all of the statements except the last. */
10042 if (warn_unused_value)
10043 {
10044 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10045 {
10046 location_t tloc;
10047 tree t = tsi_stmt (i);
10048
10049 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10050 emit_side_effect_warnings (tloc, t);
10051 }
10052 }
10053 else
10054 i = tsi_last (last);
10055 last_p = tsi_stmt_ptr (i);
10056 last = *last_p;
10057 }
10058
10059 /* If the end of the list is exception related, then the list was split
10060 by a call to push_cleanup. Continue searching. */
10061 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10062 || TREE_CODE (last) == TRY_CATCH_EXPR)
10063 {
10064 last_p = &TREE_OPERAND (last, 0);
10065 last = *last_p;
10066 goto continue_searching;
10067 }
10068
10069 if (last == error_mark_node)
10070 return last;
10071
10072 /* In the case that the BIND_EXPR is not necessary, return the
10073 expression out from inside it. */
10074 if (last == BIND_EXPR_BODY (body)
10075 && BIND_EXPR_VARS (body) == NULL)
10076 {
10077 /* Even if this looks constant, do not allow it in a constant
10078 expression. */
10079 last = c_wrap_maybe_const (last, true);
10080 /* Do not warn if the return value of a statement expression is
10081 unused. */
10082 TREE_NO_WARNING (last) = 1;
10083 return last;
10084 }
10085
10086 /* Extract the type of said expression. */
10087 type = TREE_TYPE (last);
10088
10089 /* If we're not returning a value at all, then the BIND_EXPR that
10090 we already have is a fine expression to return. */
10091 if (!type || VOID_TYPE_P (type))
10092 return body;
10093
10094 /* Now that we've located the expression containing the value, it seems
10095 silly to make voidify_wrapper_expr repeat the process. Create a
10096 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10097 tmp = create_tmp_var_raw (type);
10098
10099 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10100 tree_expr_nonnegative_p giving up immediately. */
10101 val = last;
10102 if (TREE_CODE (val) == NOP_EXPR
10103 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10104 val = TREE_OPERAND (val, 0);
10105
10106 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10107 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10108
10109 {
10110 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10111 SET_EXPR_LOCATION (t, loc);
10112 return t;
10113 }
10114 }
10115 \f
10116 /* Begin and end compound statements. This is as simple as pushing
10117 and popping new statement lists from the tree. */
10118
10119 tree
10120 c_begin_compound_stmt (bool do_scope)
10121 {
10122 tree stmt = push_stmt_list ();
10123 if (do_scope)
10124 push_scope ();
10125 return stmt;
10126 }
10127
10128 /* End a compound statement. STMT is the statement. LOC is the
10129 location of the compound statement-- this is usually the location
10130 of the opening brace. */
10131
10132 tree
10133 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10134 {
10135 tree block = NULL;
10136
10137 if (do_scope)
10138 {
10139 if (c_dialect_objc ())
10140 objc_clear_super_receiver ();
10141 block = pop_scope ();
10142 }
10143
10144 stmt = pop_stmt_list (stmt);
10145 stmt = c_build_bind_expr (loc, block, stmt);
10146
10147 /* If this compound statement is nested immediately inside a statement
10148 expression, then force a BIND_EXPR to be created. Otherwise we'll
10149 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10150 STATEMENT_LISTs merge, and thus we can lose track of what statement
10151 was really last. */
10152 if (building_stmt_list_p ()
10153 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10154 && TREE_CODE (stmt) != BIND_EXPR)
10155 {
10156 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10157 TREE_SIDE_EFFECTS (stmt) = 1;
10158 SET_EXPR_LOCATION (stmt, loc);
10159 }
10160
10161 return stmt;
10162 }
10163
10164 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10165 when the current scope is exited. EH_ONLY is true when this is not
10166 meant to apply to normal control flow transfer. */
10167
10168 void
10169 push_cleanup (tree decl, tree cleanup, bool eh_only)
10170 {
10171 enum tree_code code;
10172 tree stmt, list;
10173 bool stmt_expr;
10174
10175 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10176 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10177 add_stmt (stmt);
10178 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10179 list = push_stmt_list ();
10180 TREE_OPERAND (stmt, 0) = list;
10181 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10182 }
10183 \f
10184 /* Build a binary-operation expression without default conversions.
10185 CODE is the kind of expression to build.
10186 LOCATION is the operator's location.
10187 This function differs from `build' in several ways:
10188 the data type of the result is computed and recorded in it,
10189 warnings are generated if arg data types are invalid,
10190 special handling for addition and subtraction of pointers is known,
10191 and some optimization is done (operations on narrow ints
10192 are done in the narrower type when that gives the same result).
10193 Constant folding is also done before the result is returned.
10194
10195 Note that the operands will never have enumeral types, or function
10196 or array types, because either they will have the default conversions
10197 performed or they have both just been converted to some other type in which
10198 the arithmetic is to be done. */
10199
10200 tree
10201 build_binary_op (location_t location, enum tree_code code,
10202 tree orig_op0, tree orig_op1, int convert_p)
10203 {
10204 tree type0, type1, orig_type0, orig_type1;
10205 tree eptype;
10206 enum tree_code code0, code1;
10207 tree op0, op1;
10208 tree ret = error_mark_node;
10209 const char *invalid_op_diag;
10210 bool op0_int_operands, op1_int_operands;
10211 bool int_const, int_const_or_overflow, int_operands;
10212
10213 /* Expression code to give to the expression when it is built.
10214 Normally this is CODE, which is what the caller asked for,
10215 but in some special cases we change it. */
10216 enum tree_code resultcode = code;
10217
10218 /* Data type in which the computation is to be performed.
10219 In the simplest cases this is the common type of the arguments. */
10220 tree result_type = NULL;
10221
10222 /* When the computation is in excess precision, the type of the
10223 final EXCESS_PRECISION_EXPR. */
10224 tree semantic_result_type = NULL;
10225
10226 /* Nonzero means operands have already been type-converted
10227 in whatever way is necessary.
10228 Zero means they need to be converted to RESULT_TYPE. */
10229 int converted = 0;
10230
10231 /* Nonzero means create the expression with this type, rather than
10232 RESULT_TYPE. */
10233 tree build_type = 0;
10234
10235 /* Nonzero means after finally constructing the expression
10236 convert it to this type. */
10237 tree final_type = 0;
10238
10239 /* Nonzero if this is an operation like MIN or MAX which can
10240 safely be computed in short if both args are promoted shorts.
10241 Also implies COMMON.
10242 -1 indicates a bitwise operation; this makes a difference
10243 in the exact conditions for when it is safe to do the operation
10244 in a narrower mode. */
10245 int shorten = 0;
10246
10247 /* Nonzero if this is a comparison operation;
10248 if both args are promoted shorts, compare the original shorts.
10249 Also implies COMMON. */
10250 int short_compare = 0;
10251
10252 /* Nonzero if this is a right-shift operation, which can be computed on the
10253 original short and then promoted if the operand is a promoted short. */
10254 int short_shift = 0;
10255
10256 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10257 int common = 0;
10258
10259 /* True means types are compatible as far as ObjC is concerned. */
10260 bool objc_ok;
10261
10262 /* True means this is an arithmetic operation that may need excess
10263 precision. */
10264 bool may_need_excess_precision;
10265
10266 /* True means this is a boolean operation that converts both its
10267 operands to truth-values. */
10268 bool boolean_op = false;
10269
10270 /* Remember whether we're doing / or %. */
10271 bool doing_div_or_mod = false;
10272
10273 /* Remember whether we're doing << or >>. */
10274 bool doing_shift = false;
10275
10276 /* Tree holding instrumentation expression. */
10277 tree instrument_expr = NULL;
10278
10279 if (location == UNKNOWN_LOCATION)
10280 location = input_location;
10281
10282 op0 = orig_op0;
10283 op1 = orig_op1;
10284
10285 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10286 if (op0_int_operands)
10287 op0 = remove_c_maybe_const_expr (op0);
10288 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10289 if (op1_int_operands)
10290 op1 = remove_c_maybe_const_expr (op1);
10291 int_operands = (op0_int_operands && op1_int_operands);
10292 if (int_operands)
10293 {
10294 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10295 && TREE_CODE (orig_op1) == INTEGER_CST);
10296 int_const = (int_const_or_overflow
10297 && !TREE_OVERFLOW (orig_op0)
10298 && !TREE_OVERFLOW (orig_op1));
10299 }
10300 else
10301 int_const = int_const_or_overflow = false;
10302
10303 /* Do not apply default conversion in mixed vector/scalar expression. */
10304 if (convert_p
10305 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
10306 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
10307 {
10308 op0 = default_conversion (op0);
10309 op1 = default_conversion (op1);
10310 }
10311
10312 /* When Cilk Plus is enabled and there are array notations inside op0, then
10313 we check to see if there are builtin array notation functions. If
10314 so, then we take on the type of the array notation inside it. */
10315 if (flag_cilkplus && contains_array_notation_expr (op0))
10316 orig_type0 = type0 = find_correct_array_notation_type (op0);
10317 else
10318 orig_type0 = type0 = TREE_TYPE (op0);
10319
10320 if (flag_cilkplus && contains_array_notation_expr (op1))
10321 orig_type1 = type1 = find_correct_array_notation_type (op1);
10322 else
10323 orig_type1 = type1 = TREE_TYPE (op1);
10324
10325 /* The expression codes of the data types of the arguments tell us
10326 whether the arguments are integers, floating, pointers, etc. */
10327 code0 = TREE_CODE (type0);
10328 code1 = TREE_CODE (type1);
10329
10330 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10331 STRIP_TYPE_NOPS (op0);
10332 STRIP_TYPE_NOPS (op1);
10333
10334 /* If an error was already reported for one of the arguments,
10335 avoid reporting another error. */
10336
10337 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10338 return error_mark_node;
10339
10340 if ((invalid_op_diag
10341 = targetm.invalid_binary_op (code, type0, type1)))
10342 {
10343 error_at (location, invalid_op_diag);
10344 return error_mark_node;
10345 }
10346
10347 switch (code)
10348 {
10349 case PLUS_EXPR:
10350 case MINUS_EXPR:
10351 case MULT_EXPR:
10352 case TRUNC_DIV_EXPR:
10353 case CEIL_DIV_EXPR:
10354 case FLOOR_DIV_EXPR:
10355 case ROUND_DIV_EXPR:
10356 case EXACT_DIV_EXPR:
10357 may_need_excess_precision = true;
10358 break;
10359 default:
10360 may_need_excess_precision = false;
10361 break;
10362 }
10363 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10364 {
10365 op0 = TREE_OPERAND (op0, 0);
10366 type0 = TREE_TYPE (op0);
10367 }
10368 else if (may_need_excess_precision
10369 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10370 {
10371 type0 = eptype;
10372 op0 = convert (eptype, op0);
10373 }
10374 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10375 {
10376 op1 = TREE_OPERAND (op1, 0);
10377 type1 = TREE_TYPE (op1);
10378 }
10379 else if (may_need_excess_precision
10380 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10381 {
10382 type1 = eptype;
10383 op1 = convert (eptype, op1);
10384 }
10385
10386 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10387
10388 /* In case when one of the operands of the binary operation is
10389 a vector and another is a scalar -- convert scalar to vector. */
10390 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10391 {
10392 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10393 true);
10394
10395 switch (convert_flag)
10396 {
10397 case stv_error:
10398 return error_mark_node;
10399 case stv_firstarg:
10400 {
10401 bool maybe_const = true;
10402 tree sc;
10403 sc = c_fully_fold (op0, false, &maybe_const);
10404 sc = save_expr (sc);
10405 sc = convert (TREE_TYPE (type1), sc);
10406 op0 = build_vector_from_val (type1, sc);
10407 if (!maybe_const)
10408 op0 = c_wrap_maybe_const (op0, true);
10409 orig_type0 = type0 = TREE_TYPE (op0);
10410 code0 = TREE_CODE (type0);
10411 converted = 1;
10412 break;
10413 }
10414 case stv_secondarg:
10415 {
10416 bool maybe_const = true;
10417 tree sc;
10418 sc = c_fully_fold (op1, false, &maybe_const);
10419 sc = save_expr (sc);
10420 sc = convert (TREE_TYPE (type0), sc);
10421 op1 = build_vector_from_val (type0, sc);
10422 if (!maybe_const)
10423 op1 = c_wrap_maybe_const (op1, true);
10424 orig_type1 = type1 = TREE_TYPE (op1);
10425 code1 = TREE_CODE (type1);
10426 converted = 1;
10427 break;
10428 }
10429 default:
10430 break;
10431 }
10432 }
10433
10434 switch (code)
10435 {
10436 case PLUS_EXPR:
10437 /* Handle the pointer + int case. */
10438 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10439 {
10440 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10441 goto return_build_binary_op;
10442 }
10443 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10444 {
10445 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10446 goto return_build_binary_op;
10447 }
10448 else
10449 common = 1;
10450 break;
10451
10452 case MINUS_EXPR:
10453 /* Subtraction of two similar pointers.
10454 We must subtract them as integers, then divide by object size. */
10455 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10456 && comp_target_types (location, type0, type1))
10457 {
10458 ret = pointer_diff (location, op0, op1);
10459 goto return_build_binary_op;
10460 }
10461 /* Handle pointer minus int. Just like pointer plus int. */
10462 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10463 {
10464 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10465 goto return_build_binary_op;
10466 }
10467 else
10468 common = 1;
10469 break;
10470
10471 case MULT_EXPR:
10472 common = 1;
10473 break;
10474
10475 case TRUNC_DIV_EXPR:
10476 case CEIL_DIV_EXPR:
10477 case FLOOR_DIV_EXPR:
10478 case ROUND_DIV_EXPR:
10479 case EXACT_DIV_EXPR:
10480 doing_div_or_mod = true;
10481 warn_for_div_by_zero (location, op1);
10482
10483 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10484 || code0 == FIXED_POINT_TYPE
10485 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10486 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10487 || code1 == FIXED_POINT_TYPE
10488 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10489 {
10490 enum tree_code tcode0 = code0, tcode1 = code1;
10491
10492 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10493 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10494 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10495 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10496
10497 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10498 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10499 resultcode = RDIV_EXPR;
10500 else
10501 /* Although it would be tempting to shorten always here, that
10502 loses on some targets, since the modulo instruction is
10503 undefined if the quotient can't be represented in the
10504 computation mode. We shorten only if unsigned or if
10505 dividing by something we know != -1. */
10506 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10507 || (TREE_CODE (op1) == INTEGER_CST
10508 && !integer_all_onesp (op1)));
10509 common = 1;
10510 }
10511 break;
10512
10513 case BIT_AND_EXPR:
10514 case BIT_IOR_EXPR:
10515 case BIT_XOR_EXPR:
10516 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10517 shorten = -1;
10518 /* Allow vector types which are not floating point types. */
10519 else if (code0 == VECTOR_TYPE
10520 && code1 == VECTOR_TYPE
10521 && !VECTOR_FLOAT_TYPE_P (type0)
10522 && !VECTOR_FLOAT_TYPE_P (type1))
10523 common = 1;
10524 break;
10525
10526 case TRUNC_MOD_EXPR:
10527 case FLOOR_MOD_EXPR:
10528 doing_div_or_mod = true;
10529 warn_for_div_by_zero (location, op1);
10530
10531 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10532 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10533 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10534 common = 1;
10535 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10536 {
10537 /* Although it would be tempting to shorten always here, that loses
10538 on some targets, since the modulo instruction is undefined if the
10539 quotient can't be represented in the computation mode. We shorten
10540 only if unsigned or if dividing by something we know != -1. */
10541 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10542 || (TREE_CODE (op1) == INTEGER_CST
10543 && !integer_all_onesp (op1)));
10544 common = 1;
10545 }
10546 break;
10547
10548 case TRUTH_ANDIF_EXPR:
10549 case TRUTH_ORIF_EXPR:
10550 case TRUTH_AND_EXPR:
10551 case TRUTH_OR_EXPR:
10552 case TRUTH_XOR_EXPR:
10553 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10554 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10555 || code0 == FIXED_POINT_TYPE)
10556 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10557 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10558 || code1 == FIXED_POINT_TYPE))
10559 {
10560 /* Result of these operations is always an int,
10561 but that does not mean the operands should be
10562 converted to ints! */
10563 result_type = integer_type_node;
10564 if (op0_int_operands)
10565 {
10566 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10567 op0 = remove_c_maybe_const_expr (op0);
10568 }
10569 else
10570 op0 = c_objc_common_truthvalue_conversion (location, op0);
10571 if (op1_int_operands)
10572 {
10573 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10574 op1 = remove_c_maybe_const_expr (op1);
10575 }
10576 else
10577 op1 = c_objc_common_truthvalue_conversion (location, op1);
10578 converted = 1;
10579 boolean_op = true;
10580 }
10581 if (code == TRUTH_ANDIF_EXPR)
10582 {
10583 int_const_or_overflow = (int_operands
10584 && TREE_CODE (orig_op0) == INTEGER_CST
10585 && (op0 == truthvalue_false_node
10586 || TREE_CODE (orig_op1) == INTEGER_CST));
10587 int_const = (int_const_or_overflow
10588 && !TREE_OVERFLOW (orig_op0)
10589 && (op0 == truthvalue_false_node
10590 || !TREE_OVERFLOW (orig_op1)));
10591 }
10592 else if (code == TRUTH_ORIF_EXPR)
10593 {
10594 int_const_or_overflow = (int_operands
10595 && TREE_CODE (orig_op0) == INTEGER_CST
10596 && (op0 == truthvalue_true_node
10597 || TREE_CODE (orig_op1) == INTEGER_CST));
10598 int_const = (int_const_or_overflow
10599 && !TREE_OVERFLOW (orig_op0)
10600 && (op0 == truthvalue_true_node
10601 || !TREE_OVERFLOW (orig_op1)));
10602 }
10603 break;
10604
10605 /* Shift operations: result has same type as first operand;
10606 always convert second operand to int.
10607 Also set SHORT_SHIFT if shifting rightward. */
10608
10609 case RSHIFT_EXPR:
10610 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10611 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10612 {
10613 result_type = type0;
10614 converted = 1;
10615 }
10616 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10617 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10618 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10619 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10620 {
10621 result_type = type0;
10622 converted = 1;
10623 }
10624 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10625 && code1 == INTEGER_TYPE)
10626 {
10627 doing_shift = true;
10628 if (TREE_CODE (op1) == INTEGER_CST)
10629 {
10630 if (tree_int_cst_sgn (op1) < 0)
10631 {
10632 int_const = false;
10633 if (c_inhibit_evaluation_warnings == 0)
10634 warning_at (location, OPT_Wshift_count_negative,
10635 "right shift count is negative");
10636 }
10637 else
10638 {
10639 if (!integer_zerop (op1))
10640 short_shift = 1;
10641
10642 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10643 {
10644 int_const = false;
10645 if (c_inhibit_evaluation_warnings == 0)
10646 warning_at (location, OPT_Wshift_count_overflow,
10647 "right shift count >= width of type");
10648 }
10649 }
10650 }
10651
10652 /* Use the type of the value to be shifted. */
10653 result_type = type0;
10654 /* Avoid converting op1 to result_type later. */
10655 converted = 1;
10656 }
10657 break;
10658
10659 case LSHIFT_EXPR:
10660 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10661 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10662 {
10663 result_type = type0;
10664 converted = 1;
10665 }
10666 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10667 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10668 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10669 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10670 {
10671 result_type = type0;
10672 converted = 1;
10673 }
10674 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10675 && code1 == INTEGER_TYPE)
10676 {
10677 doing_shift = true;
10678 if (TREE_CODE (op1) == INTEGER_CST)
10679 {
10680 if (tree_int_cst_sgn (op1) < 0)
10681 {
10682 int_const = false;
10683 if (c_inhibit_evaluation_warnings == 0)
10684 warning_at (location, OPT_Wshift_count_negative,
10685 "left shift count is negative");
10686 }
10687
10688 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10689 {
10690 int_const = false;
10691 if (c_inhibit_evaluation_warnings == 0)
10692 warning_at (location, OPT_Wshift_count_overflow,
10693 "left shift count >= width of type");
10694 }
10695 }
10696
10697 /* Use the type of the value to be shifted. */
10698 result_type = type0;
10699 /* Avoid converting op1 to result_type later. */
10700 converted = 1;
10701 }
10702 break;
10703
10704 case EQ_EXPR:
10705 case NE_EXPR:
10706 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10707 {
10708 tree intt;
10709 if (!vector_types_compatible_elements_p (type0, type1))
10710 {
10711 error_at (location, "comparing vectors with different "
10712 "element types");
10713 return error_mark_node;
10714 }
10715
10716 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10717 {
10718 error_at (location, "comparing vectors with different "
10719 "number of elements");
10720 return error_mark_node;
10721 }
10722
10723 /* Always construct signed integer vector type. */
10724 intt = c_common_type_for_size (GET_MODE_BITSIZE
10725 (TYPE_MODE (TREE_TYPE (type0))), 0);
10726 result_type = build_opaque_vector_type (intt,
10727 TYPE_VECTOR_SUBPARTS (type0));
10728 converted = 1;
10729 break;
10730 }
10731 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10732 warning_at (location,
10733 OPT_Wfloat_equal,
10734 "comparing floating point with == or != is unsafe");
10735 /* Result of comparison is always int,
10736 but don't convert the args to int! */
10737 build_type = integer_type_node;
10738 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10739 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10740 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10741 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10742 short_compare = 1;
10743 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10744 {
10745 if (TREE_CODE (op0) == ADDR_EXPR
10746 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10747 {
10748 if (code == EQ_EXPR)
10749 warning_at (location,
10750 OPT_Waddress,
10751 "the comparison will always evaluate as %<false%> "
10752 "for the address of %qD will never be NULL",
10753 TREE_OPERAND (op0, 0));
10754 else
10755 warning_at (location,
10756 OPT_Waddress,
10757 "the comparison will always evaluate as %<true%> "
10758 "for the address of %qD will never be NULL",
10759 TREE_OPERAND (op0, 0));
10760 }
10761 result_type = type0;
10762 }
10763 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10764 {
10765 if (TREE_CODE (op1) == ADDR_EXPR
10766 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10767 {
10768 if (code == EQ_EXPR)
10769 warning_at (location,
10770 OPT_Waddress,
10771 "the comparison will always evaluate as %<false%> "
10772 "for the address of %qD will never be NULL",
10773 TREE_OPERAND (op1, 0));
10774 else
10775 warning_at (location,
10776 OPT_Waddress,
10777 "the comparison will always evaluate as %<true%> "
10778 "for the address of %qD will never be NULL",
10779 TREE_OPERAND (op1, 0));
10780 }
10781 result_type = type1;
10782 }
10783 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10784 {
10785 tree tt0 = TREE_TYPE (type0);
10786 tree tt1 = TREE_TYPE (type1);
10787 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10788 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10789 addr_space_t as_common = ADDR_SPACE_GENERIC;
10790
10791 /* Anything compares with void *. void * compares with anything.
10792 Otherwise, the targets must be compatible
10793 and both must be object or both incomplete. */
10794 if (comp_target_types (location, type0, type1))
10795 result_type = common_pointer_type (type0, type1);
10796 else if (!addr_space_superset (as0, as1, &as_common))
10797 {
10798 error_at (location, "comparison of pointers to "
10799 "disjoint address spaces");
10800 return error_mark_node;
10801 }
10802 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10803 {
10804 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10805 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10806 "comparison of %<void *%> with function pointer");
10807 }
10808 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10809 {
10810 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10811 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10812 "comparison of %<void *%> with function pointer");
10813 }
10814 else
10815 /* Avoid warning about the volatile ObjC EH puts on decls. */
10816 if (!objc_ok)
10817 pedwarn (location, 0,
10818 "comparison of distinct pointer types lacks a cast");
10819
10820 if (result_type == NULL_TREE)
10821 {
10822 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10823 result_type = build_pointer_type
10824 (build_qualified_type (void_type_node, qual));
10825 }
10826 }
10827 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10828 {
10829 result_type = type0;
10830 pedwarn (location, 0, "comparison between pointer and integer");
10831 }
10832 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10833 {
10834 result_type = type1;
10835 pedwarn (location, 0, "comparison between pointer and integer");
10836 }
10837 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10838 || truth_value_p (TREE_CODE (orig_op0)))
10839 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10840 || truth_value_p (TREE_CODE (orig_op1))))
10841 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10842 break;
10843
10844 case LE_EXPR:
10845 case GE_EXPR:
10846 case LT_EXPR:
10847 case GT_EXPR:
10848 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10849 {
10850 tree intt;
10851 if (!vector_types_compatible_elements_p (type0, type1))
10852 {
10853 error_at (location, "comparing vectors with different "
10854 "element types");
10855 return error_mark_node;
10856 }
10857
10858 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10859 {
10860 error_at (location, "comparing vectors with different "
10861 "number of elements");
10862 return error_mark_node;
10863 }
10864
10865 /* Always construct signed integer vector type. */
10866 intt = c_common_type_for_size (GET_MODE_BITSIZE
10867 (TYPE_MODE (TREE_TYPE (type0))), 0);
10868 result_type = build_opaque_vector_type (intt,
10869 TYPE_VECTOR_SUBPARTS (type0));
10870 converted = 1;
10871 break;
10872 }
10873 build_type = integer_type_node;
10874 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10875 || code0 == FIXED_POINT_TYPE)
10876 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10877 || code1 == FIXED_POINT_TYPE))
10878 short_compare = 1;
10879 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10880 {
10881 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10882 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10883 addr_space_t as_common;
10884
10885 if (comp_target_types (location, type0, type1))
10886 {
10887 result_type = common_pointer_type (type0, type1);
10888 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10889 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10890 pedwarn (location, 0,
10891 "comparison of complete and incomplete pointers");
10892 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10893 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10894 "ordered comparisons of pointers to functions");
10895 else if (null_pointer_constant_p (orig_op0)
10896 || null_pointer_constant_p (orig_op1))
10897 warning_at (location, OPT_Wextra,
10898 "ordered comparison of pointer with null pointer");
10899
10900 }
10901 else if (!addr_space_superset (as0, as1, &as_common))
10902 {
10903 error_at (location, "comparison of pointers to "
10904 "disjoint address spaces");
10905 return error_mark_node;
10906 }
10907 else
10908 {
10909 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10910 result_type = build_pointer_type
10911 (build_qualified_type (void_type_node, qual));
10912 pedwarn (location, 0,
10913 "comparison of distinct pointer types lacks a cast");
10914 }
10915 }
10916 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10917 {
10918 result_type = type0;
10919 if (pedantic)
10920 pedwarn (location, OPT_Wpedantic,
10921 "ordered comparison of pointer with integer zero");
10922 else if (extra_warnings)
10923 warning_at (location, OPT_Wextra,
10924 "ordered comparison of pointer with integer zero");
10925 }
10926 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10927 {
10928 result_type = type1;
10929 if (pedantic)
10930 pedwarn (location, OPT_Wpedantic,
10931 "ordered comparison of pointer with integer zero");
10932 else if (extra_warnings)
10933 warning_at (location, OPT_Wextra,
10934 "ordered comparison of pointer with integer zero");
10935 }
10936 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10937 {
10938 result_type = type0;
10939 pedwarn (location, 0, "comparison between pointer and integer");
10940 }
10941 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10942 {
10943 result_type = type1;
10944 pedwarn (location, 0, "comparison between pointer and integer");
10945 }
10946 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10947 || truth_value_p (TREE_CODE (orig_op0)))
10948 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10949 || truth_value_p (TREE_CODE (orig_op1))))
10950 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10951 break;
10952
10953 default:
10954 gcc_unreachable ();
10955 }
10956
10957 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10958 return error_mark_node;
10959
10960 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10961 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10962 || !vector_types_compatible_elements_p (type0, type1)))
10963 {
10964 binary_op_error (location, code, type0, type1);
10965 return error_mark_node;
10966 }
10967
10968 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10969 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10970 &&
10971 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10972 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10973 {
10974 bool first_complex = (code0 == COMPLEX_TYPE);
10975 bool second_complex = (code1 == COMPLEX_TYPE);
10976 int none_complex = (!first_complex && !second_complex);
10977
10978 if (shorten || common || short_compare)
10979 {
10980 result_type = c_common_type (type0, type1);
10981 do_warn_double_promotion (result_type, type0, type1,
10982 "implicit conversion from %qT to %qT "
10983 "to match other operand of binary "
10984 "expression",
10985 location);
10986 if (result_type == error_mark_node)
10987 return error_mark_node;
10988 }
10989
10990 if (first_complex != second_complex
10991 && (code == PLUS_EXPR
10992 || code == MINUS_EXPR
10993 || code == MULT_EXPR
10994 || (code == TRUNC_DIV_EXPR && first_complex))
10995 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10996 && flag_signed_zeros)
10997 {
10998 /* An operation on mixed real/complex operands must be
10999 handled specially, but the language-independent code can
11000 more easily optimize the plain complex arithmetic if
11001 -fno-signed-zeros. */
11002 tree real_type = TREE_TYPE (result_type);
11003 tree real, imag;
11004 if (type0 != orig_type0 || type1 != orig_type1)
11005 {
11006 gcc_assert (may_need_excess_precision && common);
11007 semantic_result_type = c_common_type (orig_type0, orig_type1);
11008 }
11009 if (first_complex)
11010 {
11011 if (TREE_TYPE (op0) != result_type)
11012 op0 = convert_and_check (location, result_type, op0);
11013 if (TREE_TYPE (op1) != real_type)
11014 op1 = convert_and_check (location, real_type, op1);
11015 }
11016 else
11017 {
11018 if (TREE_TYPE (op0) != real_type)
11019 op0 = convert_and_check (location, real_type, op0);
11020 if (TREE_TYPE (op1) != result_type)
11021 op1 = convert_and_check (location, result_type, op1);
11022 }
11023 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11024 return error_mark_node;
11025 if (first_complex)
11026 {
11027 op0 = c_save_expr (op0);
11028 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11029 op0, 1);
11030 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11031 op0, 1);
11032 switch (code)
11033 {
11034 case MULT_EXPR:
11035 case TRUNC_DIV_EXPR:
11036 op1 = c_save_expr (op1);
11037 imag = build2 (resultcode, real_type, imag, op1);
11038 /* Fall through. */
11039 case PLUS_EXPR:
11040 case MINUS_EXPR:
11041 real = build2 (resultcode, real_type, real, op1);
11042 break;
11043 default:
11044 gcc_unreachable();
11045 }
11046 }
11047 else
11048 {
11049 op1 = c_save_expr (op1);
11050 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11051 op1, 1);
11052 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11053 op1, 1);
11054 switch (code)
11055 {
11056 case MULT_EXPR:
11057 op0 = c_save_expr (op0);
11058 imag = build2 (resultcode, real_type, op0, imag);
11059 /* Fall through. */
11060 case PLUS_EXPR:
11061 real = build2 (resultcode, real_type, op0, real);
11062 break;
11063 case MINUS_EXPR:
11064 real = build2 (resultcode, real_type, op0, real);
11065 imag = build1 (NEGATE_EXPR, real_type, imag);
11066 break;
11067 default:
11068 gcc_unreachable();
11069 }
11070 }
11071 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11072 goto return_build_binary_op;
11073 }
11074
11075 /* For certain operations (which identify themselves by shorten != 0)
11076 if both args were extended from the same smaller type,
11077 do the arithmetic in that type and then extend.
11078
11079 shorten !=0 and !=1 indicates a bitwise operation.
11080 For them, this optimization is safe only if
11081 both args are zero-extended or both are sign-extended.
11082 Otherwise, we might change the result.
11083 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11084 but calculated in (unsigned short) it would be (unsigned short)-1. */
11085
11086 if (shorten && none_complex)
11087 {
11088 final_type = result_type;
11089 result_type = shorten_binary_op (result_type, op0, op1,
11090 shorten == -1);
11091 }
11092
11093 /* Shifts can be shortened if shifting right. */
11094
11095 if (short_shift)
11096 {
11097 int unsigned_arg;
11098 tree arg0 = get_narrower (op0, &unsigned_arg);
11099
11100 final_type = result_type;
11101
11102 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11103 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11104
11105 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11106 && tree_int_cst_sgn (op1) > 0
11107 /* We can shorten only if the shift count is less than the
11108 number of bits in the smaller type size. */
11109 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11110 /* We cannot drop an unsigned shift after sign-extension. */
11111 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11112 {
11113 /* Do an unsigned shift if the operand was zero-extended. */
11114 result_type
11115 = c_common_signed_or_unsigned_type (unsigned_arg,
11116 TREE_TYPE (arg0));
11117 /* Convert value-to-be-shifted to that type. */
11118 if (TREE_TYPE (op0) != result_type)
11119 op0 = convert (result_type, op0);
11120 converted = 1;
11121 }
11122 }
11123
11124 /* Comparison operations are shortened too but differently.
11125 They identify themselves by setting short_compare = 1. */
11126
11127 if (short_compare)
11128 {
11129 /* Don't write &op0, etc., because that would prevent op0
11130 from being kept in a register.
11131 Instead, make copies of the our local variables and
11132 pass the copies by reference, then copy them back afterward. */
11133 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11134 enum tree_code xresultcode = resultcode;
11135 tree val
11136 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11137 &xresultcode);
11138
11139 if (val != 0)
11140 {
11141 ret = val;
11142 goto return_build_binary_op;
11143 }
11144
11145 op0 = xop0, op1 = xop1;
11146 converted = 1;
11147 resultcode = xresultcode;
11148
11149 if (c_inhibit_evaluation_warnings == 0)
11150 {
11151 bool op0_maybe_const = true;
11152 bool op1_maybe_const = true;
11153 tree orig_op0_folded, orig_op1_folded;
11154
11155 if (in_late_binary_op)
11156 {
11157 orig_op0_folded = orig_op0;
11158 orig_op1_folded = orig_op1;
11159 }
11160 else
11161 {
11162 /* Fold for the sake of possible warnings, as in
11163 build_conditional_expr. This requires the
11164 "original" values to be folded, not just op0 and
11165 op1. */
11166 c_inhibit_evaluation_warnings++;
11167 op0 = c_fully_fold (op0, require_constant_value,
11168 &op0_maybe_const);
11169 op1 = c_fully_fold (op1, require_constant_value,
11170 &op1_maybe_const);
11171 c_inhibit_evaluation_warnings--;
11172 orig_op0_folded = c_fully_fold (orig_op0,
11173 require_constant_value,
11174 NULL);
11175 orig_op1_folded = c_fully_fold (orig_op1,
11176 require_constant_value,
11177 NULL);
11178 }
11179
11180 if (warn_sign_compare)
11181 warn_for_sign_compare (location, orig_op0_folded,
11182 orig_op1_folded, op0, op1,
11183 result_type, resultcode);
11184 if (!in_late_binary_op && !int_operands)
11185 {
11186 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11187 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11188 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11189 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11190 }
11191 }
11192 }
11193 }
11194
11195 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11196 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11197 Then the expression will be built.
11198 It will be given type FINAL_TYPE if that is nonzero;
11199 otherwise, it will be given type RESULT_TYPE. */
11200
11201 if (!result_type)
11202 {
11203 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
11204 return error_mark_node;
11205 }
11206
11207 if (build_type == NULL_TREE)
11208 {
11209 build_type = result_type;
11210 if ((type0 != orig_type0 || type1 != orig_type1)
11211 && !boolean_op)
11212 {
11213 gcc_assert (may_need_excess_precision && common);
11214 semantic_result_type = c_common_type (orig_type0, orig_type1);
11215 }
11216 }
11217
11218 if (!converted)
11219 {
11220 op0 = ep_convert_and_check (location, result_type, op0,
11221 semantic_result_type);
11222 op1 = ep_convert_and_check (location, result_type, op1,
11223 semantic_result_type);
11224
11225 /* This can happen if one operand has a vector type, and the other
11226 has a different type. */
11227 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11228 return error_mark_node;
11229 }
11230
11231 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11232 | SANITIZE_FLOAT_DIVIDE))
11233 && current_function_decl != 0
11234 && !lookup_attribute ("no_sanitize_undefined",
11235 DECL_ATTRIBUTES (current_function_decl))
11236 && (doing_div_or_mod || doing_shift))
11237 {
11238 /* OP0 and/or OP1 might have side-effects. */
11239 op0 = c_save_expr (op0);
11240 op1 = c_save_expr (op1);
11241 op0 = c_fully_fold (op0, false, NULL);
11242 op1 = c_fully_fold (op1, false, NULL);
11243 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11244 | SANITIZE_FLOAT_DIVIDE)))
11245 instrument_expr = ubsan_instrument_division (location, op0, op1);
11246 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11247 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11248 }
11249
11250 /* Treat expressions in initializers specially as they can't trap. */
11251 if (int_const_or_overflow)
11252 ret = (require_constant_value
11253 ? fold_build2_initializer_loc (location, resultcode, build_type,
11254 op0, op1)
11255 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11256 else
11257 ret = build2 (resultcode, build_type, op0, op1);
11258 if (final_type != 0)
11259 ret = convert (final_type, ret);
11260
11261 return_build_binary_op:
11262 gcc_assert (ret != error_mark_node);
11263 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11264 ret = (int_operands
11265 ? note_integer_operands (ret)
11266 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11267 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11268 && !in_late_binary_op)
11269 ret = note_integer_operands (ret);
11270 if (semantic_result_type)
11271 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11272 protected_set_expr_location (ret, location);
11273
11274 if (instrument_expr != NULL)
11275 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11276 instrument_expr, ret);
11277
11278 return ret;
11279 }
11280
11281
11282 /* Convert EXPR to be a truth-value, validating its type for this
11283 purpose. LOCATION is the source location for the expression. */
11284
11285 tree
11286 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11287 {
11288 bool int_const, int_operands;
11289
11290 switch (TREE_CODE (TREE_TYPE (expr)))
11291 {
11292 case ARRAY_TYPE:
11293 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11294 return error_mark_node;
11295
11296 case RECORD_TYPE:
11297 error_at (location, "used struct type value where scalar is required");
11298 return error_mark_node;
11299
11300 case UNION_TYPE:
11301 error_at (location, "used union type value where scalar is required");
11302 return error_mark_node;
11303
11304 case VOID_TYPE:
11305 error_at (location, "void value not ignored as it ought to be");
11306 return error_mark_node;
11307
11308 case FUNCTION_TYPE:
11309 gcc_unreachable ();
11310
11311 case VECTOR_TYPE:
11312 error_at (location, "used vector type where scalar is required");
11313 return error_mark_node;
11314
11315 default:
11316 break;
11317 }
11318
11319 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11320 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11321 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11322 {
11323 expr = remove_c_maybe_const_expr (expr);
11324 expr = build2 (NE_EXPR, integer_type_node, expr,
11325 convert (TREE_TYPE (expr), integer_zero_node));
11326 expr = note_integer_operands (expr);
11327 }
11328 else
11329 /* ??? Should we also give an error for vectors rather than leaving
11330 those to give errors later? */
11331 expr = c_common_truthvalue_conversion (location, expr);
11332
11333 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11334 {
11335 if (TREE_OVERFLOW (expr))
11336 return expr;
11337 else
11338 return note_integer_operands (expr);
11339 }
11340 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11341 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11342 return expr;
11343 }
11344 \f
11345
11346 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11347 required. */
11348
11349 tree
11350 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11351 {
11352 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11353 {
11354 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11355 /* Executing a compound literal inside a function reinitializes
11356 it. */
11357 if (!TREE_STATIC (decl))
11358 *se = true;
11359 return decl;
11360 }
11361 else
11362 return expr;
11363 }
11364 \f
11365 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11366 statement. LOC is the location of the OACC_PARALLEL. */
11367
11368 tree
11369 c_finish_oacc_parallel (location_t loc, tree clauses, tree block)
11370 {
11371 tree stmt;
11372
11373 block = c_end_compound_stmt (loc, block, true);
11374
11375 stmt = make_node (OACC_PARALLEL);
11376 TREE_TYPE (stmt) = void_type_node;
11377 OACC_PARALLEL_CLAUSES (stmt) = clauses;
11378 OACC_PARALLEL_BODY (stmt) = block;
11379 SET_EXPR_LOCATION (stmt, loc);
11380
11381 return add_stmt (stmt);
11382 }
11383
11384 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11385 statement. LOC is the location of the OACC_KERNELS. */
11386
11387 tree
11388 c_finish_oacc_kernels (location_t loc, tree clauses, tree block)
11389 {
11390 tree stmt;
11391
11392 block = c_end_compound_stmt (loc, block, true);
11393
11394 stmt = make_node (OACC_KERNELS);
11395 TREE_TYPE (stmt) = void_type_node;
11396 OACC_KERNELS_CLAUSES (stmt) = clauses;
11397 OACC_KERNELS_BODY (stmt) = block;
11398 SET_EXPR_LOCATION (stmt, loc);
11399
11400 return add_stmt (stmt);
11401 }
11402
11403 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11404 statement. LOC is the location of the OACC_DATA. */
11405
11406 tree
11407 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11408 {
11409 tree stmt;
11410
11411 block = c_end_compound_stmt (loc, block, true);
11412
11413 stmt = make_node (OACC_DATA);
11414 TREE_TYPE (stmt) = void_type_node;
11415 OACC_DATA_CLAUSES (stmt) = clauses;
11416 OACC_DATA_BODY (stmt) = block;
11417 SET_EXPR_LOCATION (stmt, loc);
11418
11419 return add_stmt (stmt);
11420 }
11421
11422 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11423
11424 tree
11425 c_begin_omp_parallel (void)
11426 {
11427 tree block;
11428
11429 keep_next_level ();
11430 block = c_begin_compound_stmt (true);
11431
11432 return block;
11433 }
11434
11435 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11436 statement. LOC is the location of the OMP_PARALLEL. */
11437
11438 tree
11439 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11440 {
11441 tree stmt;
11442
11443 block = c_end_compound_stmt (loc, block, true);
11444
11445 stmt = make_node (OMP_PARALLEL);
11446 TREE_TYPE (stmt) = void_type_node;
11447 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11448 OMP_PARALLEL_BODY (stmt) = block;
11449 SET_EXPR_LOCATION (stmt, loc);
11450
11451 return add_stmt (stmt);
11452 }
11453
11454 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11455
11456 tree
11457 c_begin_omp_task (void)
11458 {
11459 tree block;
11460
11461 keep_next_level ();
11462 block = c_begin_compound_stmt (true);
11463
11464 return block;
11465 }
11466
11467 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11468 statement. LOC is the location of the #pragma. */
11469
11470 tree
11471 c_finish_omp_task (location_t loc, tree clauses, tree block)
11472 {
11473 tree stmt;
11474
11475 block = c_end_compound_stmt (loc, block, true);
11476
11477 stmt = make_node (OMP_TASK);
11478 TREE_TYPE (stmt) = void_type_node;
11479 OMP_TASK_CLAUSES (stmt) = clauses;
11480 OMP_TASK_BODY (stmt) = block;
11481 SET_EXPR_LOCATION (stmt, loc);
11482
11483 return add_stmt (stmt);
11484 }
11485
11486 /* Generate GOMP_cancel call for #pragma omp cancel. */
11487
11488 void
11489 c_finish_omp_cancel (location_t loc, tree clauses)
11490 {
11491 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11492 int mask = 0;
11493 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11494 mask = 1;
11495 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11496 mask = 2;
11497 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11498 mask = 4;
11499 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11500 mask = 8;
11501 else
11502 {
11503 error_at (loc, "%<#pragma omp cancel must specify one of "
11504 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11505 "clauses");
11506 return;
11507 }
11508 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11509 if (ifc != NULL_TREE)
11510 {
11511 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11512 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11513 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11514 build_zero_cst (type));
11515 }
11516 else
11517 ifc = boolean_true_node;
11518 tree stmt = build_call_expr_loc (loc, fn, 2,
11519 build_int_cst (integer_type_node, mask),
11520 ifc);
11521 add_stmt (stmt);
11522 }
11523
11524 /* Generate GOMP_cancellation_point call for
11525 #pragma omp cancellation point. */
11526
11527 void
11528 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11529 {
11530 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11531 int mask = 0;
11532 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11533 mask = 1;
11534 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11535 mask = 2;
11536 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11537 mask = 4;
11538 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11539 mask = 8;
11540 else
11541 {
11542 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11543 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11544 "clauses");
11545 return;
11546 }
11547 tree stmt = build_call_expr_loc (loc, fn, 1,
11548 build_int_cst (integer_type_node, mask));
11549 add_stmt (stmt);
11550 }
11551
11552 /* Helper function for handle_omp_array_sections. Called recursively
11553 to handle multiple array-section-subscripts. C is the clause,
11554 T current expression (initially OMP_CLAUSE_DECL), which is either
11555 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11556 expression if specified, TREE_VALUE length expression if specified,
11557 TREE_CHAIN is what it has been specified after, or some decl.
11558 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11559 set to true if any of the array-section-subscript could have length
11560 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11561 first array-section-subscript which is known not to have length
11562 of one. Given say:
11563 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11564 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11565 all are or may have length of 1, array-section-subscript [:2] is the
11566 first one knonwn not to have length 1. For array-section-subscript
11567 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11568 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11569 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11570 case though, as some lengths could be zero. */
11571
11572 static tree
11573 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11574 bool &maybe_zero_len, unsigned int &first_non_one)
11575 {
11576 tree ret, low_bound, length, type;
11577 if (TREE_CODE (t) != TREE_LIST)
11578 {
11579 if (error_operand_p (t))
11580 return error_mark_node;
11581 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11582 {
11583 if (DECL_P (t))
11584 error_at (OMP_CLAUSE_LOCATION (c),
11585 "%qD is not a variable in %qs clause", t,
11586 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11587 else
11588 error_at (OMP_CLAUSE_LOCATION (c),
11589 "%qE is not a variable in %qs clause", t,
11590 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11591 return error_mark_node;
11592 }
11593 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11594 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11595 {
11596 error_at (OMP_CLAUSE_LOCATION (c),
11597 "%qD is threadprivate variable in %qs clause", t,
11598 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11599 return error_mark_node;
11600 }
11601 return t;
11602 }
11603
11604 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11605 maybe_zero_len, first_non_one);
11606 if (ret == error_mark_node || ret == NULL_TREE)
11607 return ret;
11608
11609 type = TREE_TYPE (ret);
11610 low_bound = TREE_PURPOSE (t);
11611 length = TREE_VALUE (t);
11612
11613 if (low_bound == error_mark_node || length == error_mark_node)
11614 return error_mark_node;
11615
11616 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11617 {
11618 error_at (OMP_CLAUSE_LOCATION (c),
11619 "low bound %qE of array section does not have integral type",
11620 low_bound);
11621 return error_mark_node;
11622 }
11623 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11624 {
11625 error_at (OMP_CLAUSE_LOCATION (c),
11626 "length %qE of array section does not have integral type",
11627 length);
11628 return error_mark_node;
11629 }
11630 if (low_bound
11631 && TREE_CODE (low_bound) == INTEGER_CST
11632 && TYPE_PRECISION (TREE_TYPE (low_bound))
11633 > TYPE_PRECISION (sizetype))
11634 low_bound = fold_convert (sizetype, low_bound);
11635 if (length
11636 && TREE_CODE (length) == INTEGER_CST
11637 && TYPE_PRECISION (TREE_TYPE (length))
11638 > TYPE_PRECISION (sizetype))
11639 length = fold_convert (sizetype, length);
11640 if (low_bound == NULL_TREE)
11641 low_bound = integer_zero_node;
11642
11643 if (length != NULL_TREE)
11644 {
11645 if (!integer_nonzerop (length))
11646 maybe_zero_len = true;
11647 if (first_non_one == types.length ()
11648 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11649 first_non_one++;
11650 }
11651 if (TREE_CODE (type) == ARRAY_TYPE)
11652 {
11653 if (length == NULL_TREE
11654 && (TYPE_DOMAIN (type) == NULL_TREE
11655 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11656 {
11657 error_at (OMP_CLAUSE_LOCATION (c),
11658 "for unknown bound array type length expression must "
11659 "be specified");
11660 return error_mark_node;
11661 }
11662 if (TREE_CODE (low_bound) == INTEGER_CST
11663 && tree_int_cst_sgn (low_bound) == -1)
11664 {
11665 error_at (OMP_CLAUSE_LOCATION (c),
11666 "negative low bound in array section in %qs clause",
11667 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11668 return error_mark_node;
11669 }
11670 if (length != NULL_TREE
11671 && TREE_CODE (length) == INTEGER_CST
11672 && tree_int_cst_sgn (length) == -1)
11673 {
11674 error_at (OMP_CLAUSE_LOCATION (c),
11675 "negative length in array section in %qs clause",
11676 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11677 return error_mark_node;
11678 }
11679 if (TYPE_DOMAIN (type)
11680 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11681 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11682 == INTEGER_CST)
11683 {
11684 tree size = size_binop (PLUS_EXPR,
11685 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11686 size_one_node);
11687 if (TREE_CODE (low_bound) == INTEGER_CST)
11688 {
11689 if (tree_int_cst_lt (size, low_bound))
11690 {
11691 error_at (OMP_CLAUSE_LOCATION (c),
11692 "low bound %qE above array section size "
11693 "in %qs clause", low_bound,
11694 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11695 return error_mark_node;
11696 }
11697 if (tree_int_cst_equal (size, low_bound))
11698 maybe_zero_len = true;
11699 else if (length == NULL_TREE
11700 && first_non_one == types.length ()
11701 && tree_int_cst_equal
11702 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11703 low_bound))
11704 first_non_one++;
11705 }
11706 else if (length == NULL_TREE)
11707 {
11708 maybe_zero_len = true;
11709 if (first_non_one == types.length ())
11710 first_non_one++;
11711 }
11712 if (length && TREE_CODE (length) == INTEGER_CST)
11713 {
11714 if (tree_int_cst_lt (size, length))
11715 {
11716 error_at (OMP_CLAUSE_LOCATION (c),
11717 "length %qE above array section size "
11718 "in %qs clause", length,
11719 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11720 return error_mark_node;
11721 }
11722 if (TREE_CODE (low_bound) == INTEGER_CST)
11723 {
11724 tree lbpluslen
11725 = size_binop (PLUS_EXPR,
11726 fold_convert (sizetype, low_bound),
11727 fold_convert (sizetype, length));
11728 if (TREE_CODE (lbpluslen) == INTEGER_CST
11729 && tree_int_cst_lt (size, lbpluslen))
11730 {
11731 error_at (OMP_CLAUSE_LOCATION (c),
11732 "high bound %qE above array section size "
11733 "in %qs clause", lbpluslen,
11734 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11735 return error_mark_node;
11736 }
11737 }
11738 }
11739 }
11740 else if (length == NULL_TREE)
11741 {
11742 maybe_zero_len = true;
11743 if (first_non_one == types.length ())
11744 first_non_one++;
11745 }
11746
11747 /* For [lb:] we will need to evaluate lb more than once. */
11748 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11749 {
11750 tree lb = c_save_expr (low_bound);
11751 if (lb != low_bound)
11752 {
11753 TREE_PURPOSE (t) = lb;
11754 low_bound = lb;
11755 }
11756 }
11757 }
11758 else if (TREE_CODE (type) == POINTER_TYPE)
11759 {
11760 if (length == NULL_TREE)
11761 {
11762 error_at (OMP_CLAUSE_LOCATION (c),
11763 "for pointer type length expression must be specified");
11764 return error_mark_node;
11765 }
11766 /* If there is a pointer type anywhere but in the very first
11767 array-section-subscript, the array section can't be contiguous. */
11768 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11769 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11770 {
11771 error_at (OMP_CLAUSE_LOCATION (c),
11772 "array section is not contiguous in %qs clause",
11773 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11774 return error_mark_node;
11775 }
11776 }
11777 else
11778 {
11779 error_at (OMP_CLAUSE_LOCATION (c),
11780 "%qE does not have pointer or array type", ret);
11781 return error_mark_node;
11782 }
11783 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11784 types.safe_push (TREE_TYPE (ret));
11785 /* We will need to evaluate lb more than once. */
11786 tree lb = c_save_expr (low_bound);
11787 if (lb != low_bound)
11788 {
11789 TREE_PURPOSE (t) = lb;
11790 low_bound = lb;
11791 }
11792 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11793 return ret;
11794 }
11795
11796 /* Handle array sections for clause C. */
11797
11798 static bool
11799 handle_omp_array_sections (tree c)
11800 {
11801 bool maybe_zero_len = false;
11802 unsigned int first_non_one = 0;
11803 vec<tree> types = vNULL;
11804 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11805 maybe_zero_len, first_non_one);
11806 if (first == error_mark_node)
11807 {
11808 types.release ();
11809 return true;
11810 }
11811 if (first == NULL_TREE)
11812 {
11813 types.release ();
11814 return false;
11815 }
11816 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11817 {
11818 tree t = OMP_CLAUSE_DECL (c);
11819 tree tem = NULL_TREE;
11820 types.release ();
11821 /* Need to evaluate side effects in the length expressions
11822 if any. */
11823 while (TREE_CODE (t) == TREE_LIST)
11824 {
11825 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11826 {
11827 if (tem == NULL_TREE)
11828 tem = TREE_VALUE (t);
11829 else
11830 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11831 TREE_VALUE (t), tem);
11832 }
11833 t = TREE_CHAIN (t);
11834 }
11835 if (tem)
11836 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11837 first = c_fully_fold (first, false, NULL);
11838 OMP_CLAUSE_DECL (c) = first;
11839 }
11840 else
11841 {
11842 unsigned int num = types.length (), i;
11843 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11844 tree condition = NULL_TREE;
11845
11846 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11847 maybe_zero_len = true;
11848
11849 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11850 t = TREE_CHAIN (t))
11851 {
11852 tree low_bound = TREE_PURPOSE (t);
11853 tree length = TREE_VALUE (t);
11854
11855 i--;
11856 if (low_bound
11857 && TREE_CODE (low_bound) == INTEGER_CST
11858 && TYPE_PRECISION (TREE_TYPE (low_bound))
11859 > TYPE_PRECISION (sizetype))
11860 low_bound = fold_convert (sizetype, low_bound);
11861 if (length
11862 && TREE_CODE (length) == INTEGER_CST
11863 && TYPE_PRECISION (TREE_TYPE (length))
11864 > TYPE_PRECISION (sizetype))
11865 length = fold_convert (sizetype, length);
11866 if (low_bound == NULL_TREE)
11867 low_bound = integer_zero_node;
11868 if (!maybe_zero_len && i > first_non_one)
11869 {
11870 if (integer_nonzerop (low_bound))
11871 goto do_warn_noncontiguous;
11872 if (length != NULL_TREE
11873 && TREE_CODE (length) == INTEGER_CST
11874 && TYPE_DOMAIN (types[i])
11875 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11876 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11877 == INTEGER_CST)
11878 {
11879 tree size;
11880 size = size_binop (PLUS_EXPR,
11881 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11882 size_one_node);
11883 if (!tree_int_cst_equal (length, size))
11884 {
11885 do_warn_noncontiguous:
11886 error_at (OMP_CLAUSE_LOCATION (c),
11887 "array section is not contiguous in %qs "
11888 "clause",
11889 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11890 types.release ();
11891 return true;
11892 }
11893 }
11894 if (length != NULL_TREE
11895 && TREE_SIDE_EFFECTS (length))
11896 {
11897 if (side_effects == NULL_TREE)
11898 side_effects = length;
11899 else
11900 side_effects = build2 (COMPOUND_EXPR,
11901 TREE_TYPE (side_effects),
11902 length, side_effects);
11903 }
11904 }
11905 else
11906 {
11907 tree l;
11908
11909 if (i > first_non_one && length && integer_nonzerop (length))
11910 continue;
11911 if (length)
11912 l = fold_convert (sizetype, length);
11913 else
11914 {
11915 l = size_binop (PLUS_EXPR,
11916 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11917 size_one_node);
11918 l = size_binop (MINUS_EXPR, l,
11919 fold_convert (sizetype, low_bound));
11920 }
11921 if (i > first_non_one)
11922 {
11923 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11924 size_zero_node);
11925 if (condition == NULL_TREE)
11926 condition = l;
11927 else
11928 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11929 l, condition);
11930 }
11931 else if (size == NULL_TREE)
11932 {
11933 size = size_in_bytes (TREE_TYPE (types[i]));
11934 size = size_binop (MULT_EXPR, size, l);
11935 if (condition)
11936 size = fold_build3 (COND_EXPR, sizetype, condition,
11937 size, size_zero_node);
11938 }
11939 else
11940 size = size_binop (MULT_EXPR, size, l);
11941 }
11942 }
11943 types.release ();
11944 if (side_effects)
11945 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11946 first = c_fully_fold (first, false, NULL);
11947 OMP_CLAUSE_DECL (c) = first;
11948 if (size)
11949 size = c_fully_fold (size, false, NULL);
11950 OMP_CLAUSE_SIZE (c) = size;
11951 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11952 return false;
11953 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
11954 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11955 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
11956 if (!c_mark_addressable (t))
11957 return false;
11958 OMP_CLAUSE_DECL (c2) = t;
11959 t = build_fold_addr_expr (first);
11960 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11961 tree ptr = OMP_CLAUSE_DECL (c2);
11962 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11963 ptr = build_fold_addr_expr (ptr);
11964 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11965 ptrdiff_type_node, t,
11966 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11967 ptrdiff_type_node, ptr));
11968 t = c_fully_fold (t, false, NULL);
11969 OMP_CLAUSE_SIZE (c2) = t;
11970 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11971 OMP_CLAUSE_CHAIN (c) = c2;
11972 }
11973 return false;
11974 }
11975
11976 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11977 an inline call. But, remap
11978 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11979 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11980
11981 static tree
11982 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11983 tree decl, tree placeholder)
11984 {
11985 copy_body_data id;
11986 hash_map<tree, tree> decl_map;
11987
11988 decl_map.put (omp_decl1, placeholder);
11989 decl_map.put (omp_decl2, decl);
11990 memset (&id, 0, sizeof (id));
11991 id.src_fn = DECL_CONTEXT (omp_decl1);
11992 id.dst_fn = current_function_decl;
11993 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11994 id.decl_map = &decl_map;
11995
11996 id.copy_decl = copy_decl_no_change;
11997 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11998 id.transform_new_cfg = true;
11999 id.transform_return_to_modify = false;
12000 id.transform_lang_insert_block = NULL;
12001 id.eh_lp_nr = 0;
12002 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12003 return stmt;
12004 }
12005
12006 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12007 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12008
12009 static tree
12010 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12011 {
12012 if (*tp == (tree) data)
12013 return *tp;
12014 return NULL_TREE;
12015 }
12016
12017 /* For all elements of CLAUSES, validate them against their constraints.
12018 Remove any elements from the list that are invalid. */
12019
12020 tree
12021 c_finish_omp_clauses (tree clauses)
12022 {
12023 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12024 bitmap_head aligned_head;
12025 tree c, t, *pc;
12026 bool branch_seen = false;
12027 bool copyprivate_seen = false;
12028 tree *nowait_clause = NULL;
12029
12030 bitmap_obstack_initialize (NULL);
12031 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12032 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12033 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12034 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12035
12036 for (pc = &clauses, c = clauses; c ; c = *pc)
12037 {
12038 bool remove = false;
12039 bool need_complete = false;
12040 bool need_implicitly_determined = false;
12041
12042 switch (OMP_CLAUSE_CODE (c))
12043 {
12044 case OMP_CLAUSE_SHARED:
12045 need_implicitly_determined = true;
12046 goto check_dup_generic;
12047
12048 case OMP_CLAUSE_PRIVATE:
12049 need_complete = true;
12050 need_implicitly_determined = true;
12051 goto check_dup_generic;
12052
12053 case OMP_CLAUSE_REDUCTION:
12054 need_implicitly_determined = true;
12055 t = OMP_CLAUSE_DECL (c);
12056 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12057 && (FLOAT_TYPE_P (TREE_TYPE (t))
12058 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
12059 {
12060 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12061 const char *r_name = NULL;
12062
12063 switch (r_code)
12064 {
12065 case PLUS_EXPR:
12066 case MULT_EXPR:
12067 case MINUS_EXPR:
12068 break;
12069 case MIN_EXPR:
12070 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12071 r_name = "min";
12072 break;
12073 case MAX_EXPR:
12074 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12075 r_name = "max";
12076 break;
12077 case BIT_AND_EXPR:
12078 r_name = "&";
12079 break;
12080 case BIT_XOR_EXPR:
12081 r_name = "^";
12082 break;
12083 case BIT_IOR_EXPR:
12084 r_name = "|";
12085 break;
12086 case TRUTH_ANDIF_EXPR:
12087 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12088 r_name = "&&";
12089 break;
12090 case TRUTH_ORIF_EXPR:
12091 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12092 r_name = "||";
12093 break;
12094 default:
12095 gcc_unreachable ();
12096 }
12097 if (r_name)
12098 {
12099 error_at (OMP_CLAUSE_LOCATION (c),
12100 "%qE has invalid type for %<reduction(%s)%>",
12101 t, r_name);
12102 remove = true;
12103 break;
12104 }
12105 }
12106 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12107 {
12108 error_at (OMP_CLAUSE_LOCATION (c),
12109 "user defined reduction not found for %qD", t);
12110 remove = true;
12111 break;
12112 }
12113 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12114 {
12115 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12116 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12117 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12118 VAR_DECL, NULL_TREE, type);
12119 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12120 DECL_ARTIFICIAL (placeholder) = 1;
12121 DECL_IGNORED_P (placeholder) = 1;
12122 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12123 c_mark_addressable (placeholder);
12124 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12125 c_mark_addressable (OMP_CLAUSE_DECL (c));
12126 OMP_CLAUSE_REDUCTION_MERGE (c)
12127 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12128 TREE_VEC_ELT (list, 0),
12129 TREE_VEC_ELT (list, 1),
12130 OMP_CLAUSE_DECL (c), placeholder);
12131 OMP_CLAUSE_REDUCTION_MERGE (c)
12132 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12133 void_type_node, NULL_TREE,
12134 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12135 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12136 if (TREE_VEC_LENGTH (list) == 6)
12137 {
12138 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12139 c_mark_addressable (OMP_CLAUSE_DECL (c));
12140 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12141 c_mark_addressable (placeholder);
12142 tree init = TREE_VEC_ELT (list, 5);
12143 if (init == error_mark_node)
12144 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12145 OMP_CLAUSE_REDUCTION_INIT (c)
12146 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12147 TREE_VEC_ELT (list, 3),
12148 OMP_CLAUSE_DECL (c), placeholder);
12149 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12150 OMP_CLAUSE_REDUCTION_INIT (c)
12151 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12152 OMP_CLAUSE_REDUCTION_INIT (c));
12153 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12154 c_find_omp_placeholder_r,
12155 placeholder, NULL))
12156 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12157 }
12158 else
12159 {
12160 tree init;
12161 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12162 init = build_constructor (TREE_TYPE (t), NULL);
12163 else
12164 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12165 OMP_CLAUSE_REDUCTION_INIT (c)
12166 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12167 }
12168 OMP_CLAUSE_REDUCTION_INIT (c)
12169 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12170 void_type_node, NULL_TREE,
12171 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12172 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12173 }
12174 goto check_dup_generic;
12175
12176 case OMP_CLAUSE_COPYPRIVATE:
12177 copyprivate_seen = true;
12178 if (nowait_clause)
12179 {
12180 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12181 "%<nowait%> clause must not be used together "
12182 "with %<copyprivate%>");
12183 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12184 nowait_clause = NULL;
12185 }
12186 goto check_dup_generic;
12187
12188 case OMP_CLAUSE_COPYIN:
12189 t = OMP_CLAUSE_DECL (c);
12190 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
12191 {
12192 error_at (OMP_CLAUSE_LOCATION (c),
12193 "%qE must be %<threadprivate%> for %<copyin%>", t);
12194 remove = true;
12195 break;
12196 }
12197 goto check_dup_generic;
12198
12199 case OMP_CLAUSE_LINEAR:
12200 t = OMP_CLAUSE_DECL (c);
12201 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12202 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12203 {
12204 error_at (OMP_CLAUSE_LOCATION (c),
12205 "linear clause applied to non-integral non-pointer "
12206 "variable with type %qT", TREE_TYPE (t));
12207 remove = true;
12208 break;
12209 }
12210 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12211 {
12212 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12213 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12214 OMP_CLAUSE_DECL (c), s);
12215 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12216 sizetype, s, OMP_CLAUSE_DECL (c));
12217 if (s == error_mark_node)
12218 s = size_one_node;
12219 OMP_CLAUSE_LINEAR_STEP (c) = s;
12220 }
12221 else
12222 OMP_CLAUSE_LINEAR_STEP (c)
12223 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12224 goto check_dup_generic;
12225
12226 check_dup_generic:
12227 t = OMP_CLAUSE_DECL (c);
12228 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12229 {
12230 error_at (OMP_CLAUSE_LOCATION (c),
12231 "%qE is not a variable in clause %qs", t,
12232 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12233 remove = true;
12234 }
12235 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12236 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12237 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12238 {
12239 error_at (OMP_CLAUSE_LOCATION (c),
12240 "%qE appears more than once in data clauses", t);
12241 remove = true;
12242 }
12243 else
12244 bitmap_set_bit (&generic_head, DECL_UID (t));
12245 break;
12246
12247 case OMP_CLAUSE_FIRSTPRIVATE:
12248 t = OMP_CLAUSE_DECL (c);
12249 need_complete = true;
12250 need_implicitly_determined = true;
12251 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12252 {
12253 error_at (OMP_CLAUSE_LOCATION (c),
12254 "%qE is not a variable in clause %<firstprivate%>", t);
12255 remove = true;
12256 }
12257 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12258 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12259 {
12260 error_at (OMP_CLAUSE_LOCATION (c),
12261 "%qE appears more than once in data clauses", t);
12262 remove = true;
12263 }
12264 else
12265 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12266 break;
12267
12268 case OMP_CLAUSE_LASTPRIVATE:
12269 t = OMP_CLAUSE_DECL (c);
12270 need_complete = true;
12271 need_implicitly_determined = true;
12272 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12273 {
12274 error_at (OMP_CLAUSE_LOCATION (c),
12275 "%qE is not a variable in clause %<lastprivate%>", t);
12276 remove = true;
12277 }
12278 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12279 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12280 {
12281 error_at (OMP_CLAUSE_LOCATION (c),
12282 "%qE appears more than once in data clauses", t);
12283 remove = true;
12284 }
12285 else
12286 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12287 break;
12288
12289 case OMP_CLAUSE_ALIGNED:
12290 t = OMP_CLAUSE_DECL (c);
12291 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12292 {
12293 error_at (OMP_CLAUSE_LOCATION (c),
12294 "%qE is not a variable in %<aligned%> clause", t);
12295 remove = true;
12296 }
12297 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12298 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12299 {
12300 error_at (OMP_CLAUSE_LOCATION (c),
12301 "%qE in %<aligned%> clause is neither a pointer nor "
12302 "an array", t);
12303 remove = true;
12304 }
12305 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12306 {
12307 error_at (OMP_CLAUSE_LOCATION (c),
12308 "%qE appears more than once in %<aligned%> clauses",
12309 t);
12310 remove = true;
12311 }
12312 else
12313 bitmap_set_bit (&aligned_head, DECL_UID (t));
12314 break;
12315
12316 case OMP_CLAUSE_DEPEND:
12317 t = OMP_CLAUSE_DECL (c);
12318 if (TREE_CODE (t) == TREE_LIST)
12319 {
12320 if (handle_omp_array_sections (c))
12321 remove = true;
12322 break;
12323 }
12324 if (t == error_mark_node)
12325 remove = true;
12326 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12327 {
12328 error_at (OMP_CLAUSE_LOCATION (c),
12329 "%qE is not a variable in %<depend%> clause", t);
12330 remove = true;
12331 }
12332 else if (!c_mark_addressable (t))
12333 remove = true;
12334 break;
12335
12336 case OMP_CLAUSE_MAP:
12337 case OMP_CLAUSE_TO:
12338 case OMP_CLAUSE_FROM:
12339 case OMP_CLAUSE__CACHE_:
12340 t = OMP_CLAUSE_DECL (c);
12341 if (TREE_CODE (t) == TREE_LIST)
12342 {
12343 if (handle_omp_array_sections (c))
12344 remove = true;
12345 else
12346 {
12347 t = OMP_CLAUSE_DECL (c);
12348 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12349 {
12350 error_at (OMP_CLAUSE_LOCATION (c),
12351 "array section does not have mappable type "
12352 "in %qs clause",
12353 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12354 remove = true;
12355 }
12356 }
12357 break;
12358 }
12359 if (t == error_mark_node)
12360 remove = true;
12361 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12362 {
12363 error_at (OMP_CLAUSE_LOCATION (c),
12364 "%qE is not a variable in %qs clause", t,
12365 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12366 remove = true;
12367 }
12368 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12369 {
12370 error_at (OMP_CLAUSE_LOCATION (c),
12371 "%qD is threadprivate variable in %qs clause", t,
12372 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12373 remove = true;
12374 }
12375 else if (!c_mark_addressable (t))
12376 remove = true;
12377 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12378 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12379 || (OMP_CLAUSE_MAP_KIND (c)
12380 == GOMP_MAP_FORCE_DEVICEPTR)))
12381 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12382 {
12383 error_at (OMP_CLAUSE_LOCATION (c),
12384 "%qD does not have a mappable type in %qs clause", t,
12385 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12386 remove = true;
12387 }
12388 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12389 {
12390 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12391 error ("%qD appears more than once in motion clauses", t);
12392 else
12393 error ("%qD appears more than once in map clauses", t);
12394 remove = true;
12395 }
12396 else
12397 bitmap_set_bit (&generic_head, DECL_UID (t));
12398 break;
12399
12400 case OMP_CLAUSE_UNIFORM:
12401 t = OMP_CLAUSE_DECL (c);
12402 if (TREE_CODE (t) != PARM_DECL)
12403 {
12404 if (DECL_P (t))
12405 error_at (OMP_CLAUSE_LOCATION (c),
12406 "%qD is not an argument in %<uniform%> clause", t);
12407 else
12408 error_at (OMP_CLAUSE_LOCATION (c),
12409 "%qE is not an argument in %<uniform%> clause", t);
12410 remove = true;
12411 break;
12412 }
12413 goto check_dup_generic;
12414
12415 case OMP_CLAUSE_NOWAIT:
12416 if (copyprivate_seen)
12417 {
12418 error_at (OMP_CLAUSE_LOCATION (c),
12419 "%<nowait%> clause must not be used together "
12420 "with %<copyprivate%>");
12421 remove = true;
12422 break;
12423 }
12424 nowait_clause = pc;
12425 pc = &OMP_CLAUSE_CHAIN (c);
12426 continue;
12427
12428 case OMP_CLAUSE_IF:
12429 case OMP_CLAUSE_NUM_THREADS:
12430 case OMP_CLAUSE_NUM_TEAMS:
12431 case OMP_CLAUSE_THREAD_LIMIT:
12432 case OMP_CLAUSE_SCHEDULE:
12433 case OMP_CLAUSE_ORDERED:
12434 case OMP_CLAUSE_DEFAULT:
12435 case OMP_CLAUSE_UNTIED:
12436 case OMP_CLAUSE_COLLAPSE:
12437 case OMP_CLAUSE_FINAL:
12438 case OMP_CLAUSE_MERGEABLE:
12439 case OMP_CLAUSE_SAFELEN:
12440 case OMP_CLAUSE_SIMDLEN:
12441 case OMP_CLAUSE_DEVICE:
12442 case OMP_CLAUSE_DIST_SCHEDULE:
12443 case OMP_CLAUSE_PARALLEL:
12444 case OMP_CLAUSE_FOR:
12445 case OMP_CLAUSE_SECTIONS:
12446 case OMP_CLAUSE_TASKGROUP:
12447 case OMP_CLAUSE_PROC_BIND:
12448 case OMP_CLAUSE__CILK_FOR_COUNT_:
12449 case OMP_CLAUSE_NUM_GANGS:
12450 case OMP_CLAUSE_NUM_WORKERS:
12451 case OMP_CLAUSE_VECTOR_LENGTH:
12452 case OMP_CLAUSE_ASYNC:
12453 case OMP_CLAUSE_WAIT:
12454 case OMP_CLAUSE_AUTO:
12455 case OMP_CLAUSE_SEQ:
12456 case OMP_CLAUSE_GANG:
12457 case OMP_CLAUSE_WORKER:
12458 case OMP_CLAUSE_VECTOR:
12459 pc = &OMP_CLAUSE_CHAIN (c);
12460 continue;
12461
12462 case OMP_CLAUSE_INBRANCH:
12463 case OMP_CLAUSE_NOTINBRANCH:
12464 if (branch_seen)
12465 {
12466 error_at (OMP_CLAUSE_LOCATION (c),
12467 "%<inbranch%> clause is incompatible with "
12468 "%<notinbranch%>");
12469 remove = true;
12470 break;
12471 }
12472 branch_seen = true;
12473 pc = &OMP_CLAUSE_CHAIN (c);
12474 continue;
12475
12476 default:
12477 gcc_unreachable ();
12478 }
12479
12480 if (!remove)
12481 {
12482 t = OMP_CLAUSE_DECL (c);
12483
12484 if (need_complete)
12485 {
12486 t = require_complete_type (t);
12487 if (t == error_mark_node)
12488 remove = true;
12489 }
12490
12491 if (need_implicitly_determined)
12492 {
12493 const char *share_name = NULL;
12494
12495 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12496 share_name = "threadprivate";
12497 else switch (c_omp_predetermined_sharing (t))
12498 {
12499 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12500 break;
12501 case OMP_CLAUSE_DEFAULT_SHARED:
12502 /* const vars may be specified in firstprivate clause. */
12503 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12504 && TREE_READONLY (t))
12505 break;
12506 share_name = "shared";
12507 break;
12508 case OMP_CLAUSE_DEFAULT_PRIVATE:
12509 share_name = "private";
12510 break;
12511 default:
12512 gcc_unreachable ();
12513 }
12514 if (share_name)
12515 {
12516 error_at (OMP_CLAUSE_LOCATION (c),
12517 "%qE is predetermined %qs for %qs",
12518 t, share_name,
12519 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12520 remove = true;
12521 }
12522 }
12523 }
12524
12525 if (remove)
12526 *pc = OMP_CLAUSE_CHAIN (c);
12527 else
12528 pc = &OMP_CLAUSE_CHAIN (c);
12529 }
12530
12531 bitmap_obstack_release (NULL);
12532 return clauses;
12533 }
12534
12535 /* Create a transaction node. */
12536
12537 tree
12538 c_finish_transaction (location_t loc, tree block, int flags)
12539 {
12540 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12541 if (flags & TM_STMT_ATTR_OUTER)
12542 TRANSACTION_EXPR_OUTER (stmt) = 1;
12543 if (flags & TM_STMT_ATTR_RELAXED)
12544 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12545 return add_stmt (stmt);
12546 }
12547
12548 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12549 down to the element type of an array. */
12550
12551 tree
12552 c_build_qualified_type (tree type, int type_quals)
12553 {
12554 if (type == error_mark_node)
12555 return type;
12556
12557 if (TREE_CODE (type) == ARRAY_TYPE)
12558 {
12559 tree t;
12560 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12561 type_quals);
12562
12563 /* See if we already have an identically qualified type. */
12564 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12565 {
12566 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12567 && TYPE_NAME (t) == TYPE_NAME (type)
12568 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12569 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12570 TYPE_ATTRIBUTES (type)))
12571 break;
12572 }
12573 if (!t)
12574 {
12575 tree domain = TYPE_DOMAIN (type);
12576
12577 t = build_variant_type_copy (type);
12578 TREE_TYPE (t) = element_type;
12579
12580 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12581 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12582 SET_TYPE_STRUCTURAL_EQUALITY (t);
12583 else if (TYPE_CANONICAL (element_type) != element_type
12584 || (domain && TYPE_CANONICAL (domain) != domain))
12585 {
12586 tree unqualified_canon
12587 = build_array_type (TYPE_CANONICAL (element_type),
12588 domain? TYPE_CANONICAL (domain)
12589 : NULL_TREE);
12590 TYPE_CANONICAL (t)
12591 = c_build_qualified_type (unqualified_canon, type_quals);
12592 }
12593 else
12594 TYPE_CANONICAL (t) = t;
12595 }
12596 return t;
12597 }
12598
12599 /* A restrict-qualified pointer type must be a pointer to object or
12600 incomplete type. Note that the use of POINTER_TYPE_P also allows
12601 REFERENCE_TYPEs, which is appropriate for C++. */
12602 if ((type_quals & TYPE_QUAL_RESTRICT)
12603 && (!POINTER_TYPE_P (type)
12604 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12605 {
12606 error ("invalid use of %<restrict%>");
12607 type_quals &= ~TYPE_QUAL_RESTRICT;
12608 }
12609
12610 return build_qualified_type (type, type_quals);
12611 }
12612
12613 /* Build a VA_ARG_EXPR for the C parser. */
12614
12615 tree
12616 c_build_va_arg (location_t loc, tree expr, tree type)
12617 {
12618 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12619 warning_at (loc, OPT_Wc___compat,
12620 "C++ requires promoted type, not enum type, in %<va_arg%>");
12621 return build_va_arg (loc, expr, type);
12622 }
12623
12624 /* Return truthvalue of whether T1 is the same tree structure as T2.
12625 Return 1 if they are the same. Return 0 if they are different. */
12626
12627 bool
12628 c_tree_equal (tree t1, tree t2)
12629 {
12630 enum tree_code code1, code2;
12631
12632 if (t1 == t2)
12633 return true;
12634 if (!t1 || !t2)
12635 return false;
12636
12637 for (code1 = TREE_CODE (t1);
12638 CONVERT_EXPR_CODE_P (code1)
12639 || code1 == NON_LVALUE_EXPR;
12640 code1 = TREE_CODE (t1))
12641 t1 = TREE_OPERAND (t1, 0);
12642 for (code2 = TREE_CODE (t2);
12643 CONVERT_EXPR_CODE_P (code2)
12644 || code2 == NON_LVALUE_EXPR;
12645 code2 = TREE_CODE (t2))
12646 t2 = TREE_OPERAND (t2, 0);
12647
12648 /* They might have become equal now. */
12649 if (t1 == t2)
12650 return true;
12651
12652 if (code1 != code2)
12653 return false;
12654
12655 switch (code1)
12656 {
12657 case INTEGER_CST:
12658 return wi::eq_p (t1, t2);
12659
12660 case REAL_CST:
12661 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12662
12663 case STRING_CST:
12664 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12665 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12666 TREE_STRING_LENGTH (t1));
12667
12668 case FIXED_CST:
12669 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12670 TREE_FIXED_CST (t2));
12671
12672 case COMPLEX_CST:
12673 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12674 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12675
12676 case VECTOR_CST:
12677 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12678
12679 case CONSTRUCTOR:
12680 /* We need to do this when determining whether or not two
12681 non-type pointer to member function template arguments
12682 are the same. */
12683 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12684 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12685 return false;
12686 {
12687 tree field, value;
12688 unsigned int i;
12689 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12690 {
12691 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12692 if (!c_tree_equal (field, elt2->index)
12693 || !c_tree_equal (value, elt2->value))
12694 return false;
12695 }
12696 }
12697 return true;
12698
12699 case TREE_LIST:
12700 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12701 return false;
12702 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12703 return false;
12704 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12705
12706 case SAVE_EXPR:
12707 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12708
12709 case CALL_EXPR:
12710 {
12711 tree arg1, arg2;
12712 call_expr_arg_iterator iter1, iter2;
12713 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12714 return false;
12715 for (arg1 = first_call_expr_arg (t1, &iter1),
12716 arg2 = first_call_expr_arg (t2, &iter2);
12717 arg1 && arg2;
12718 arg1 = next_call_expr_arg (&iter1),
12719 arg2 = next_call_expr_arg (&iter2))
12720 if (!c_tree_equal (arg1, arg2))
12721 return false;
12722 if (arg1 || arg2)
12723 return false;
12724 return true;
12725 }
12726
12727 case TARGET_EXPR:
12728 {
12729 tree o1 = TREE_OPERAND (t1, 0);
12730 tree o2 = TREE_OPERAND (t2, 0);
12731
12732 /* Special case: if either target is an unallocated VAR_DECL,
12733 it means that it's going to be unified with whatever the
12734 TARGET_EXPR is really supposed to initialize, so treat it
12735 as being equivalent to anything. */
12736 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12737 && !DECL_RTL_SET_P (o1))
12738 /*Nop*/;
12739 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12740 && !DECL_RTL_SET_P (o2))
12741 /*Nop*/;
12742 else if (!c_tree_equal (o1, o2))
12743 return false;
12744
12745 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12746 }
12747
12748 case COMPONENT_REF:
12749 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12750 return false;
12751 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12752
12753 case PARM_DECL:
12754 case VAR_DECL:
12755 case CONST_DECL:
12756 case FIELD_DECL:
12757 case FUNCTION_DECL:
12758 case IDENTIFIER_NODE:
12759 case SSA_NAME:
12760 return false;
12761
12762 case TREE_VEC:
12763 {
12764 unsigned ix;
12765 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12766 return false;
12767 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12768 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12769 TREE_VEC_ELT (t2, ix)))
12770 return false;
12771 return true;
12772 }
12773
12774 default:
12775 break;
12776 }
12777
12778 switch (TREE_CODE_CLASS (code1))
12779 {
12780 case tcc_unary:
12781 case tcc_binary:
12782 case tcc_comparison:
12783 case tcc_expression:
12784 case tcc_vl_exp:
12785 case tcc_reference:
12786 case tcc_statement:
12787 {
12788 int i, n = TREE_OPERAND_LENGTH (t1);
12789
12790 switch (code1)
12791 {
12792 case PREINCREMENT_EXPR:
12793 case PREDECREMENT_EXPR:
12794 case POSTINCREMENT_EXPR:
12795 case POSTDECREMENT_EXPR:
12796 n = 1;
12797 break;
12798 case ARRAY_REF:
12799 n = 2;
12800 break;
12801 default:
12802 break;
12803 }
12804
12805 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12806 && n != TREE_OPERAND_LENGTH (t2))
12807 return false;
12808
12809 for (i = 0; i < n; ++i)
12810 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12811 return false;
12812
12813 return true;
12814 }
12815
12816 case tcc_type:
12817 return comptypes (t1, t2);
12818 default:
12819 gcc_unreachable ();
12820 }
12821 /* We can get here with --disable-checking. */
12822 return false;
12823 }
12824
12825 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12826 spawn-helper and BODY is the newly created body for FNDECL. */
12827
12828 void
12829 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12830 {
12831 tree list = alloc_stmt_list ();
12832 tree frame = make_cilk_frame (fndecl);
12833 tree dtor = create_cilk_function_exit (frame, false, true);
12834 add_local_decl (cfun, frame);
12835
12836 DECL_SAVED_TREE (fndecl) = list;
12837 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12838 frame);
12839 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12840 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12841
12842 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12843 append_to_statement_list (detach_expr, &body_list);
12844
12845 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12846 body = fold_build_cleanup_point_expr (void_type_node, body);
12847
12848 append_to_statement_list (body, &body_list);
12849 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12850 body_list, dtor), &list);
12851 }
This page took 0.589688 seconds and 6 git commands to generate.