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