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