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