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