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