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