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