]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c/c-typeck.c
Do UBSAN sanitization just when current_function_decl != NULL_TREE (PR sanitize/81530).
[thirdparty/gcc.git] / gcc / c / c-typeck.c
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2017 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 "memmodel.h"
30 #include "target.h"
31 #include "function.h"
32 #include "bitmap.h"
33 #include "c-tree.h"
34 #include "gimple-expr.h"
35 #include "predict.h"
36 #include "stor-layout.h"
37 #include "trans-mem.h"
38 #include "varasm.h"
39 #include "stmt.h"
40 #include "langhooks.h"
41 #include "c-lang.h"
42 #include "intl.h"
43 #include "tree-iterator.h"
44 #include "gimplify.h"
45 #include "tree-inline.h"
46 #include "omp-general.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-ubsan.h"
49 #include "cilk.h"
50 #include "gomp-constants.h"
51 #include "spellcheck-tree.h"
52 #include "gcc-rich-location.h"
53 #include "asan.h"
54
55 /* Possible cases of implicit bad conversions. Used to select
56 diagnostic messages in convert_for_assignment. */
57 enum impl_conv {
58 ic_argpass,
59 ic_assign,
60 ic_init,
61 ic_return
62 };
63
64 /* The level of nesting inside "__alignof__". */
65 int in_alignof;
66
67 /* The level of nesting inside "sizeof". */
68 int in_sizeof;
69
70 /* The level of nesting inside "typeof". */
71 int in_typeof;
72
73 /* The argument of last parsed sizeof expression, only to be tested
74 if expr.original_code == SIZEOF_EXPR. */
75 tree c_last_sizeof_arg;
76 location_t c_last_sizeof_loc;
77
78 /* Nonzero if we might need to print a "missing braces around
79 initializer" message within this initializer. */
80 static int found_missing_braces;
81
82 static int require_constant_value;
83 static int require_constant_elements;
84
85 static bool null_pointer_constant_p (const_tree);
86 static tree qualify_type (tree, tree);
87 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
88 bool *);
89 static int comp_target_types (location_t, tree, tree);
90 static int function_types_compatible_p (const_tree, const_tree, bool *,
91 bool *);
92 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
93 static tree lookup_field (tree, tree);
94 static int convert_arguments (location_t, vec<location_t>, tree,
95 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
96 tree);
97 static tree pointer_diff (location_t, tree, tree);
98 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
99 enum impl_conv, bool, tree, tree, int);
100 static tree valid_compound_expr_initializer (tree, tree);
101 static void push_string (const char *);
102 static void push_member_name (tree);
103 static int spelling_length (void);
104 static char *print_spelling (char *);
105 static void warning_init (location_t, int, const char *);
106 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
107 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
108 bool, struct obstack *);
109 static void output_pending_init_elements (int, struct obstack *);
110 static bool set_designator (location_t, int, struct obstack *);
111 static void push_range_stack (tree, struct obstack *);
112 static void add_pending_init (location_t, tree, tree, tree, bool,
113 struct obstack *);
114 static void set_nonincremental_init (struct obstack *);
115 static void set_nonincremental_init_from_string (tree, struct obstack *);
116 static tree find_init_member (tree, struct obstack *);
117 static void readonly_warning (tree, enum lvalue_use);
118 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
119 static void record_maybe_used_decl (tree);
120 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
121 \f
122 /* Return true if EXP is a null pointer constant, false otherwise. */
123
124 static bool
125 null_pointer_constant_p (const_tree expr)
126 {
127 /* This should really operate on c_expr structures, but they aren't
128 yet available everywhere required. */
129 tree type = TREE_TYPE (expr);
130 return (TREE_CODE (expr) == INTEGER_CST
131 && !TREE_OVERFLOW (expr)
132 && integer_zerop (expr)
133 && (INTEGRAL_TYPE_P (type)
134 || (TREE_CODE (type) == POINTER_TYPE
135 && VOID_TYPE_P (TREE_TYPE (type))
136 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
137 }
138
139 /* EXPR may appear in an unevaluated part of an integer constant
140 expression, but not in an evaluated part. Wrap it in a
141 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
142 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
143
144 static tree
145 note_integer_operands (tree expr)
146 {
147 tree ret;
148 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
149 {
150 ret = copy_node (expr);
151 TREE_OVERFLOW (ret) = 1;
152 }
153 else
154 {
155 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
156 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
157 }
158 return ret;
159 }
160
161 /* Having checked whether EXPR may appear in an unevaluated part of an
162 integer constant expression and found that it may, remove any
163 C_MAYBE_CONST_EXPR noting this fact and return the resulting
164 expression. */
165
166 static inline tree
167 remove_c_maybe_const_expr (tree expr)
168 {
169 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
170 return C_MAYBE_CONST_EXPR_EXPR (expr);
171 else
172 return expr;
173 }
174
175 \f/* This is a cache to hold if two types are compatible or not. */
176
177 struct tagged_tu_seen_cache {
178 const struct tagged_tu_seen_cache * next;
179 const_tree t1;
180 const_tree t2;
181 /* The return value of tagged_types_tu_compatible_p if we had seen
182 these two types already. */
183 int val;
184 };
185
186 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
187 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
188
189 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
190 does not have an incomplete type. (That includes void types.)
191 LOC is the location of the use. */
192
193 tree
194 require_complete_type (location_t loc, tree value)
195 {
196 tree type = TREE_TYPE (value);
197
198 if (error_operand_p (value))
199 return error_mark_node;
200
201 /* First, detect a valid value with a complete type. */
202 if (COMPLETE_TYPE_P (type))
203 return value;
204
205 c_incomplete_type_error (loc, value, type);
206 return error_mark_node;
207 }
208
209 /* Print an error message for invalid use of an incomplete type.
210 VALUE is the expression that was used (or 0 if that isn't known)
211 and TYPE is the type that was invalid. LOC is the location for
212 the error. */
213
214 void
215 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
216 {
217 /* Avoid duplicate error message. */
218 if (TREE_CODE (type) == ERROR_MARK)
219 return;
220
221 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
222 error_at (loc, "%qD has an incomplete type %qT", value, type);
223 else
224 {
225 retry:
226 /* We must print an error message. Be clever about what it says. */
227
228 switch (TREE_CODE (type))
229 {
230 case RECORD_TYPE:
231 case UNION_TYPE:
232 case ENUMERAL_TYPE:
233 break;
234
235 case VOID_TYPE:
236 error_at (loc, "invalid use of void expression");
237 return;
238
239 case ARRAY_TYPE:
240 if (TYPE_DOMAIN (type))
241 {
242 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
243 {
244 error_at (loc, "invalid use of flexible array member");
245 return;
246 }
247 type = TREE_TYPE (type);
248 goto retry;
249 }
250 error_at (loc, "invalid use of array with unspecified bounds");
251 return;
252
253 default:
254 gcc_unreachable ();
255 }
256
257 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
258 error_at (loc, "invalid use of undefined type %qT", type);
259 else
260 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
261 error_at (loc, "invalid use of incomplete typedef %qT", type);
262 }
263 }
264
265 /* Given a type, apply default promotions wrt unnamed function
266 arguments and return the new type. */
267
268 tree
269 c_type_promotes_to (tree type)
270 {
271 tree ret = NULL_TREE;
272
273 if (TYPE_MAIN_VARIANT (type) == float_type_node)
274 ret = double_type_node;
275 else if (c_promoting_integer_type_p (type))
276 {
277 /* Preserve unsignedness if not really getting any wider. */
278 if (TYPE_UNSIGNED (type)
279 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
280 ret = unsigned_type_node;
281 else
282 ret = integer_type_node;
283 }
284
285 if (ret != NULL_TREE)
286 return (TYPE_ATOMIC (type)
287 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
288 : ret);
289
290 return type;
291 }
292
293 /* Return true if between two named address spaces, whether there is a superset
294 named address space that encompasses both address spaces. If there is a
295 superset, return which address space is the superset. */
296
297 static bool
298 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
299 {
300 if (as1 == as2)
301 {
302 *common = as1;
303 return true;
304 }
305 else if (targetm.addr_space.subset_p (as1, as2))
306 {
307 *common = as2;
308 return true;
309 }
310 else if (targetm.addr_space.subset_p (as2, as1))
311 {
312 *common = as1;
313 return true;
314 }
315 else
316 return false;
317 }
318
319 /* Return a variant of TYPE which has all the type qualifiers of LIKE
320 as well as those of TYPE. */
321
322 static tree
323 qualify_type (tree type, tree like)
324 {
325 addr_space_t as_type = TYPE_ADDR_SPACE (type);
326 addr_space_t as_like = TYPE_ADDR_SPACE (like);
327 addr_space_t as_common;
328
329 /* If the two named address spaces are different, determine the common
330 superset address space. If there isn't one, raise an error. */
331 if (!addr_space_superset (as_type, as_like, &as_common))
332 {
333 as_common = as_type;
334 error ("%qT and %qT are in disjoint named address spaces",
335 type, like);
336 }
337
338 return c_build_qualified_type (type,
339 TYPE_QUALS_NO_ADDR_SPACE (type)
340 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
341 | ENCODE_QUAL_ADDR_SPACE (as_common));
342 }
343
344 /* Return true iff the given tree T is a variable length array. */
345
346 bool
347 c_vla_type_p (const_tree t)
348 {
349 if (TREE_CODE (t) == ARRAY_TYPE
350 && C_TYPE_VARIABLE_SIZE (t))
351 return true;
352 return false;
353 }
354 \f
355 /* Return the composite type of two compatible types.
356
357 We assume that comptypes has already been done and returned
358 nonzero; if that isn't so, this may crash. In particular, we
359 assume that qualifiers match. */
360
361 tree
362 composite_type (tree t1, tree t2)
363 {
364 enum tree_code code1;
365 enum tree_code code2;
366 tree attributes;
367
368 /* Save time if the two types are the same. */
369
370 if (t1 == t2) return t1;
371
372 /* If one type is nonsense, use the other. */
373 if (t1 == error_mark_node)
374 return t2;
375 if (t2 == error_mark_node)
376 return t1;
377
378 code1 = TREE_CODE (t1);
379 code2 = TREE_CODE (t2);
380
381 /* Merge the attributes. */
382 attributes = targetm.merge_type_attributes (t1, t2);
383
384 /* If one is an enumerated type and the other is the compatible
385 integer type, the composite type might be either of the two
386 (DR#013 question 3). For consistency, use the enumerated type as
387 the composite type. */
388
389 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
390 return t1;
391 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
392 return t2;
393
394 gcc_assert (code1 == code2);
395
396 switch (code1)
397 {
398 case POINTER_TYPE:
399 /* For two pointers, do this recursively on the target type. */
400 {
401 tree pointed_to_1 = TREE_TYPE (t1);
402 tree pointed_to_2 = TREE_TYPE (t2);
403 tree target = composite_type (pointed_to_1, pointed_to_2);
404 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
405 t1 = build_type_attribute_variant (t1, attributes);
406 return qualify_type (t1, t2);
407 }
408
409 case ARRAY_TYPE:
410 {
411 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
412 int quals;
413 tree unqual_elt;
414 tree d1 = TYPE_DOMAIN (t1);
415 tree d2 = TYPE_DOMAIN (t2);
416 bool d1_variable, d2_variable;
417 bool d1_zero, d2_zero;
418 bool t1_complete, t2_complete;
419
420 /* We should not have any type quals on arrays at all. */
421 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
422 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
423
424 t1_complete = COMPLETE_TYPE_P (t1);
425 t2_complete = COMPLETE_TYPE_P (t2);
426
427 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
428 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
429
430 d1_variable = (!d1_zero
431 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
432 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
433 d2_variable = (!d2_zero
434 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
435 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
436 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
437 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
438
439 /* Save space: see if the result is identical to one of the args. */
440 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
441 && (d2_variable || d2_zero || !d1_variable))
442 return build_type_attribute_variant (t1, attributes);
443 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
444 && (d1_variable || d1_zero || !d2_variable))
445 return build_type_attribute_variant (t2, attributes);
446
447 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
448 return build_type_attribute_variant (t1, attributes);
449 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
450 return build_type_attribute_variant (t2, attributes);
451
452 /* Merge the element types, and have a size if either arg has
453 one. We may have qualifiers on the element types. To set
454 up TYPE_MAIN_VARIANT correctly, we need to form the
455 composite of the unqualified types and add the qualifiers
456 back at the end. */
457 quals = TYPE_QUALS (strip_array_types (elt));
458 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
459 t1 = build_array_type (unqual_elt,
460 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
461 && (d2_variable
462 || d2_zero
463 || !d1_variable))
464 ? t1
465 : t2));
466 /* Ensure a composite type involving a zero-length array type
467 is a zero-length type not an incomplete type. */
468 if (d1_zero && d2_zero
469 && (t1_complete || t2_complete)
470 && !COMPLETE_TYPE_P (t1))
471 {
472 TYPE_SIZE (t1) = bitsize_zero_node;
473 TYPE_SIZE_UNIT (t1) = size_zero_node;
474 }
475 t1 = c_build_qualified_type (t1, quals);
476 return build_type_attribute_variant (t1, attributes);
477 }
478
479 case ENUMERAL_TYPE:
480 case RECORD_TYPE:
481 case UNION_TYPE:
482 if (attributes != NULL)
483 {
484 /* Try harder not to create a new aggregate type. */
485 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
486 return t1;
487 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
488 return t2;
489 }
490 return build_type_attribute_variant (t1, attributes);
491
492 case FUNCTION_TYPE:
493 /* Function types: prefer the one that specified arg types.
494 If both do, merge the arg types. Also merge the return types. */
495 {
496 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
497 tree p1 = TYPE_ARG_TYPES (t1);
498 tree p2 = TYPE_ARG_TYPES (t2);
499 int len;
500 tree newargs, n;
501 int i;
502
503 /* Save space: see if the result is identical to one of the args. */
504 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
505 return build_type_attribute_variant (t1, attributes);
506 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
507 return build_type_attribute_variant (t2, attributes);
508
509 /* Simple way if one arg fails to specify argument types. */
510 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
511 {
512 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
513 t1 = build_type_attribute_variant (t1, attributes);
514 return qualify_type (t1, t2);
515 }
516 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
517 {
518 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
519 t1 = build_type_attribute_variant (t1, attributes);
520 return qualify_type (t1, t2);
521 }
522
523 /* If both args specify argument types, we must merge the two
524 lists, argument by argument. */
525
526 for (len = 0, newargs = p1;
527 newargs && newargs != void_list_node;
528 len++, newargs = TREE_CHAIN (newargs))
529 ;
530
531 for (i = 0; i < len; i++)
532 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
533
534 n = newargs;
535
536 for (; p1 && p1 != void_list_node;
537 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
538 {
539 /* A null type means arg type is not specified.
540 Take whatever the other function type has. */
541 if (TREE_VALUE (p1) == NULL_TREE)
542 {
543 TREE_VALUE (n) = TREE_VALUE (p2);
544 goto parm_done;
545 }
546 if (TREE_VALUE (p2) == NULL_TREE)
547 {
548 TREE_VALUE (n) = TREE_VALUE (p1);
549 goto parm_done;
550 }
551
552 /* Given wait (union {union wait *u; int *i} *)
553 and wait (union wait *),
554 prefer union wait * as type of parm. */
555 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
556 && TREE_VALUE (p1) != TREE_VALUE (p2))
557 {
558 tree memb;
559 tree mv2 = TREE_VALUE (p2);
560 if (mv2 && mv2 != error_mark_node
561 && TREE_CODE (mv2) != ARRAY_TYPE)
562 mv2 = TYPE_MAIN_VARIANT (mv2);
563 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
564 memb; memb = DECL_CHAIN (memb))
565 {
566 tree mv3 = TREE_TYPE (memb);
567 if (mv3 && mv3 != error_mark_node
568 && TREE_CODE (mv3) != ARRAY_TYPE)
569 mv3 = TYPE_MAIN_VARIANT (mv3);
570 if (comptypes (mv3, mv2))
571 {
572 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
573 TREE_VALUE (p2));
574 pedwarn (input_location, OPT_Wpedantic,
575 "function types not truly compatible in ISO C");
576 goto parm_done;
577 }
578 }
579 }
580 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
581 && TREE_VALUE (p2) != TREE_VALUE (p1))
582 {
583 tree memb;
584 tree mv1 = TREE_VALUE (p1);
585 if (mv1 && mv1 != error_mark_node
586 && TREE_CODE (mv1) != ARRAY_TYPE)
587 mv1 = TYPE_MAIN_VARIANT (mv1);
588 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
589 memb; memb = DECL_CHAIN (memb))
590 {
591 tree mv3 = TREE_TYPE (memb);
592 if (mv3 && mv3 != error_mark_node
593 && TREE_CODE (mv3) != ARRAY_TYPE)
594 mv3 = TYPE_MAIN_VARIANT (mv3);
595 if (comptypes (mv3, mv1))
596 {
597 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
598 TREE_VALUE (p1));
599 pedwarn (input_location, OPT_Wpedantic,
600 "function types not truly compatible in ISO C");
601 goto parm_done;
602 }
603 }
604 }
605 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
606 parm_done: ;
607 }
608
609 t1 = build_function_type (valtype, newargs);
610 t1 = qualify_type (t1, t2);
611 }
612 /* FALLTHRU */
613
614 default:
615 return build_type_attribute_variant (t1, attributes);
616 }
617
618 }
619
620 /* Return the type of a conditional expression between pointers to
621 possibly differently qualified versions of compatible types.
622
623 We assume that comp_target_types has already been done and returned
624 nonzero; if that isn't so, this may crash. */
625
626 static tree
627 common_pointer_type (tree t1, tree t2)
628 {
629 tree attributes;
630 tree pointed_to_1, mv1;
631 tree pointed_to_2, mv2;
632 tree target;
633 unsigned target_quals;
634 addr_space_t as1, as2, as_common;
635 int quals1, quals2;
636
637 /* Save time if the two types are the same. */
638
639 if (t1 == t2) return t1;
640
641 /* If one type is nonsense, use the other. */
642 if (t1 == error_mark_node)
643 return t2;
644 if (t2 == error_mark_node)
645 return t1;
646
647 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
648 && TREE_CODE (t2) == POINTER_TYPE);
649
650 /* Merge the attributes. */
651 attributes = targetm.merge_type_attributes (t1, t2);
652
653 /* Find the composite type of the target types, and combine the
654 qualifiers of the two types' targets. Do not lose qualifiers on
655 array element types by taking the TYPE_MAIN_VARIANT. */
656 mv1 = pointed_to_1 = TREE_TYPE (t1);
657 mv2 = pointed_to_2 = TREE_TYPE (t2);
658 if (TREE_CODE (mv1) != ARRAY_TYPE)
659 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
660 if (TREE_CODE (mv2) != ARRAY_TYPE)
661 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
662 target = composite_type (mv1, mv2);
663
664 /* Strip array types to get correct qualifier for pointers to arrays */
665 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
666 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
667
668 /* For function types do not merge const qualifiers, but drop them
669 if used inconsistently. The middle-end uses these to mark const
670 and noreturn functions. */
671 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
672 target_quals = (quals1 & quals2);
673 else
674 target_quals = (quals1 | quals2);
675
676 /* If the two named address spaces are different, determine the common
677 superset address space. This is guaranteed to exist due to the
678 assumption that comp_target_type returned non-zero. */
679 as1 = TYPE_ADDR_SPACE (pointed_to_1);
680 as2 = TYPE_ADDR_SPACE (pointed_to_2);
681 if (!addr_space_superset (as1, as2, &as_common))
682 gcc_unreachable ();
683
684 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
685
686 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
687 return build_type_attribute_variant (t1, attributes);
688 }
689
690 /* Return the common type for two arithmetic types under the usual
691 arithmetic conversions. The default conversions have already been
692 applied, and enumerated types converted to their compatible integer
693 types. The resulting type is unqualified and has no attributes.
694
695 This is the type for the result of most arithmetic operations
696 if the operands have the given two types. */
697
698 static tree
699 c_common_type (tree t1, tree t2)
700 {
701 enum tree_code code1;
702 enum tree_code code2;
703
704 /* If one type is nonsense, use the other. */
705 if (t1 == error_mark_node)
706 return t2;
707 if (t2 == error_mark_node)
708 return t1;
709
710 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
711 t1 = TYPE_MAIN_VARIANT (t1);
712
713 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
714 t2 = TYPE_MAIN_VARIANT (t2);
715
716 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
717 t1 = build_type_attribute_variant (t1, NULL_TREE);
718
719 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
720 t2 = build_type_attribute_variant (t2, NULL_TREE);
721
722 /* Save time if the two types are the same. */
723
724 if (t1 == t2) return t1;
725
726 code1 = TREE_CODE (t1);
727 code2 = TREE_CODE (t2);
728
729 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
730 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
731 || code1 == INTEGER_TYPE);
732 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
733 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
734 || code2 == INTEGER_TYPE);
735
736 /* When one operand is a decimal float type, the other operand cannot be
737 a generic float type or a complex type. We also disallow vector types
738 here. */
739 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
740 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
741 {
742 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
743 {
744 error ("can%'t mix operands of decimal float and vector types");
745 return error_mark_node;
746 }
747 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
748 {
749 error ("can%'t mix operands of decimal float and complex types");
750 return error_mark_node;
751 }
752 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
753 {
754 error ("can%'t mix operands of decimal float and other float types");
755 return error_mark_node;
756 }
757 }
758
759 /* If one type is a vector type, return that type. (How the usual
760 arithmetic conversions apply to the vector types extension is not
761 precisely specified.) */
762 if (code1 == VECTOR_TYPE)
763 return t1;
764
765 if (code2 == VECTOR_TYPE)
766 return t2;
767
768 /* If one type is complex, form the common type of the non-complex
769 components, then make that complex. Use T1 or T2 if it is the
770 required type. */
771 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
772 {
773 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
774 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
775 tree subtype = c_common_type (subtype1, subtype2);
776
777 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
778 return t1;
779 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
780 return t2;
781 else
782 return build_complex_type (subtype);
783 }
784
785 /* If only one is real, use it as the result. */
786
787 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
788 return t1;
789
790 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
791 return t2;
792
793 /* If both are real and either are decimal floating point types, use
794 the decimal floating point type with the greater precision. */
795
796 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
797 {
798 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
799 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
800 return dfloat128_type_node;
801 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
802 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
803 return dfloat64_type_node;
804 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
805 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
806 return dfloat32_type_node;
807 }
808
809 /* Deal with fixed-point types. */
810 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
811 {
812 unsigned int unsignedp = 0, satp = 0;
813 machine_mode m1, m2;
814 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
815
816 m1 = TYPE_MODE (t1);
817 m2 = TYPE_MODE (t2);
818
819 /* If one input type is saturating, the result type is saturating. */
820 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
821 satp = 1;
822
823 /* If both fixed-point types are unsigned, the result type is unsigned.
824 When mixing fixed-point and integer types, follow the sign of the
825 fixed-point type.
826 Otherwise, the result type is signed. */
827 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
828 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
829 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
830 && TYPE_UNSIGNED (t1))
831 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
832 && TYPE_UNSIGNED (t2)))
833 unsignedp = 1;
834
835 /* The result type is signed. */
836 if (unsignedp == 0)
837 {
838 /* If the input type is unsigned, we need to convert to the
839 signed type. */
840 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
841 {
842 enum mode_class mclass = (enum mode_class) 0;
843 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
844 mclass = MODE_FRACT;
845 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
846 mclass = MODE_ACCUM;
847 else
848 gcc_unreachable ();
849 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
850 }
851 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
852 {
853 enum mode_class mclass = (enum mode_class) 0;
854 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
855 mclass = MODE_FRACT;
856 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
857 mclass = MODE_ACCUM;
858 else
859 gcc_unreachable ();
860 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
861 }
862 }
863
864 if (code1 == FIXED_POINT_TYPE)
865 {
866 fbit1 = GET_MODE_FBIT (m1);
867 ibit1 = GET_MODE_IBIT (m1);
868 }
869 else
870 {
871 fbit1 = 0;
872 /* Signed integers need to subtract one sign bit. */
873 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
874 }
875
876 if (code2 == FIXED_POINT_TYPE)
877 {
878 fbit2 = GET_MODE_FBIT (m2);
879 ibit2 = GET_MODE_IBIT (m2);
880 }
881 else
882 {
883 fbit2 = 0;
884 /* Signed integers need to subtract one sign bit. */
885 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
886 }
887
888 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
889 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
890 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
891 satp);
892 }
893
894 /* Both real or both integers; use the one with greater precision. */
895
896 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
897 return t1;
898 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
899 return t2;
900
901 /* Same precision. Prefer long longs to longs to ints when the
902 same precision, following the C99 rules on integer type rank
903 (which are equivalent to the C90 rules for C90 types). */
904
905 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
906 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
907 return long_long_unsigned_type_node;
908
909 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
910 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
911 {
912 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
913 return long_long_unsigned_type_node;
914 else
915 return long_long_integer_type_node;
916 }
917
918 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
919 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
920 return long_unsigned_type_node;
921
922 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
923 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
924 {
925 /* But preserve unsignedness from the other type,
926 since long cannot hold all the values of an unsigned int. */
927 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
928 return long_unsigned_type_node;
929 else
930 return long_integer_type_node;
931 }
932
933 /* For floating types of the same TYPE_PRECISION (which we here
934 assume means either the same set of values, or sets of values
935 neither a subset of the other, with behavior being undefined in
936 the latter case), follow the rules from TS 18661-3: prefer
937 interchange types _FloatN, then standard types long double,
938 double, float, then extended types _FloatNx. For extended types,
939 check them starting with _Float128x as that seems most consistent
940 in spirit with preferring long double to double; for interchange
941 types, also check in that order for consistency although it's not
942 possible for more than one of them to have the same
943 precision. */
944 tree mv1 = TYPE_MAIN_VARIANT (t1);
945 tree mv2 = TYPE_MAIN_VARIANT (t2);
946
947 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
948 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
949 return FLOATN_TYPE_NODE (i);
950
951 /* Likewise, prefer long double to double even if same size. */
952 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
953 return long_double_type_node;
954
955 /* Likewise, prefer double to float even if same size.
956 We got a couple of embedded targets with 32 bit doubles, and the
957 pdp11 might have 64 bit floats. */
958 if (mv1 == double_type_node || mv2 == double_type_node)
959 return double_type_node;
960
961 if (mv1 == float_type_node || mv2 == float_type_node)
962 return float_type_node;
963
964 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
965 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
966 return FLOATNX_TYPE_NODE (i);
967
968 /* Otherwise prefer the unsigned one. */
969
970 if (TYPE_UNSIGNED (t1))
971 return t1;
972 else
973 return t2;
974 }
975 \f
976 /* Wrapper around c_common_type that is used by c-common.c and other
977 front end optimizations that remove promotions. ENUMERAL_TYPEs
978 are allowed here and are converted to their compatible integer types.
979 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
980 preferably a non-Boolean type as the common type. */
981 tree
982 common_type (tree t1, tree t2)
983 {
984 if (TREE_CODE (t1) == ENUMERAL_TYPE)
985 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
986 if (TREE_CODE (t2) == ENUMERAL_TYPE)
987 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
988
989 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
990 if (TREE_CODE (t1) == BOOLEAN_TYPE
991 && TREE_CODE (t2) == BOOLEAN_TYPE)
992 return boolean_type_node;
993
994 /* If either type is BOOLEAN_TYPE, then return the other. */
995 if (TREE_CODE (t1) == BOOLEAN_TYPE)
996 return t2;
997 if (TREE_CODE (t2) == BOOLEAN_TYPE)
998 return t1;
999
1000 return c_common_type (t1, t2);
1001 }
1002
1003 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1004 or various other operations. Return 2 if they are compatible
1005 but a warning may be needed if you use them together. */
1006
1007 int
1008 comptypes (tree type1, tree type2)
1009 {
1010 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1011 int val;
1012
1013 val = comptypes_internal (type1, type2, NULL, NULL);
1014 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1015
1016 return val;
1017 }
1018
1019 /* Like comptypes, but if it returns non-zero because enum and int are
1020 compatible, it sets *ENUM_AND_INT_P to true. */
1021
1022 static int
1023 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1024 {
1025 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1026 int val;
1027
1028 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1029 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1030
1031 return val;
1032 }
1033
1034 /* Like comptypes, but if it returns nonzero for different types, it
1035 sets *DIFFERENT_TYPES_P to true. */
1036
1037 int
1038 comptypes_check_different_types (tree type1, tree type2,
1039 bool *different_types_p)
1040 {
1041 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1042 int val;
1043
1044 val = comptypes_internal (type1, type2, NULL, different_types_p);
1045 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1046
1047 return val;
1048 }
1049 \f
1050 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1051 or various other operations. Return 2 if they are compatible
1052 but a warning may be needed if you use them together. If
1053 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1054 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1055 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1056 NULL, and the types are compatible but different enough not to be
1057 permitted in C11 typedef redeclarations, then this sets
1058 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1059 false, but may or may not be set if the types are incompatible.
1060 This differs from comptypes, in that we don't free the seen
1061 types. */
1062
1063 static int
1064 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1065 bool *different_types_p)
1066 {
1067 const_tree t1 = type1;
1068 const_tree t2 = type2;
1069 int attrval, val;
1070
1071 /* Suppress errors caused by previously reported errors. */
1072
1073 if (t1 == t2 || !t1 || !t2
1074 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1075 return 1;
1076
1077 /* Enumerated types are compatible with integer types, but this is
1078 not transitive: two enumerated types in the same translation unit
1079 are compatible with each other only if they are the same type. */
1080
1081 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1082 {
1083 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1084 if (TREE_CODE (t2) != VOID_TYPE)
1085 {
1086 if (enum_and_int_p != NULL)
1087 *enum_and_int_p = true;
1088 if (different_types_p != NULL)
1089 *different_types_p = true;
1090 }
1091 }
1092 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1093 {
1094 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1095 if (TREE_CODE (t1) != VOID_TYPE)
1096 {
1097 if (enum_and_int_p != NULL)
1098 *enum_and_int_p = true;
1099 if (different_types_p != NULL)
1100 *different_types_p = true;
1101 }
1102 }
1103
1104 if (t1 == t2)
1105 return 1;
1106
1107 /* Different classes of types can't be compatible. */
1108
1109 if (TREE_CODE (t1) != TREE_CODE (t2))
1110 return 0;
1111
1112 /* Qualifiers must match. C99 6.7.3p9 */
1113
1114 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1115 return 0;
1116
1117 /* Allow for two different type nodes which have essentially the same
1118 definition. Note that we already checked for equality of the type
1119 qualifiers (just above). */
1120
1121 if (TREE_CODE (t1) != ARRAY_TYPE
1122 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1123 return 1;
1124
1125 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1126 if (!(attrval = comp_type_attributes (t1, t2)))
1127 return 0;
1128
1129 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1130 val = 0;
1131
1132 switch (TREE_CODE (t1))
1133 {
1134 case INTEGER_TYPE:
1135 case FIXED_POINT_TYPE:
1136 case REAL_TYPE:
1137 /* With these nodes, we can't determine type equivalence by
1138 looking at what is stored in the nodes themselves, because
1139 two nodes might have different TYPE_MAIN_VARIANTs but still
1140 represent the same type. For example, wchar_t and int could
1141 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1142 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1143 and are distinct types. On the other hand, int and the
1144 following typedef
1145
1146 typedef int INT __attribute((may_alias));
1147
1148 have identical properties, different TYPE_MAIN_VARIANTs, but
1149 represent the same type. The canonical type system keeps
1150 track of equivalence in this case, so we fall back on it. */
1151 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1152
1153 case POINTER_TYPE:
1154 /* Do not remove mode information. */
1155 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1156 break;
1157 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1158 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1159 enum_and_int_p, different_types_p));
1160 break;
1161
1162 case FUNCTION_TYPE:
1163 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1164 different_types_p);
1165 break;
1166
1167 case ARRAY_TYPE:
1168 {
1169 tree d1 = TYPE_DOMAIN (t1);
1170 tree d2 = TYPE_DOMAIN (t2);
1171 bool d1_variable, d2_variable;
1172 bool d1_zero, d2_zero;
1173 val = 1;
1174
1175 /* Target types must match incl. qualifiers. */
1176 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1177 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1178 enum_and_int_p,
1179 different_types_p)))
1180 return 0;
1181
1182 if (different_types_p != NULL
1183 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1184 *different_types_p = true;
1185 /* Sizes must match unless one is missing or variable. */
1186 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1187 break;
1188
1189 d1_zero = !TYPE_MAX_VALUE (d1);
1190 d2_zero = !TYPE_MAX_VALUE (d2);
1191
1192 d1_variable = (!d1_zero
1193 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1194 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1195 d2_variable = (!d2_zero
1196 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1197 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1198 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1199 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1200
1201 if (different_types_p != NULL
1202 && d1_variable != d2_variable)
1203 *different_types_p = true;
1204 if (d1_variable || d2_variable)
1205 break;
1206 if (d1_zero && d2_zero)
1207 break;
1208 if (d1_zero || d2_zero
1209 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1210 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1211 val = 0;
1212
1213 break;
1214 }
1215
1216 case ENUMERAL_TYPE:
1217 case RECORD_TYPE:
1218 case UNION_TYPE:
1219 if (val != 1 && !same_translation_unit_p (t1, t2))
1220 {
1221 tree a1 = TYPE_ATTRIBUTES (t1);
1222 tree a2 = TYPE_ATTRIBUTES (t2);
1223
1224 if (! attribute_list_contained (a1, a2)
1225 && ! attribute_list_contained (a2, a1))
1226 break;
1227
1228 if (attrval != 2)
1229 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1230 different_types_p);
1231 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1232 different_types_p);
1233 }
1234 break;
1235
1236 case VECTOR_TYPE:
1237 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1238 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1239 enum_and_int_p, different_types_p));
1240 break;
1241
1242 default:
1243 break;
1244 }
1245 return attrval == 2 && val == 1 ? 2 : val;
1246 }
1247
1248 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1249 their qualifiers, except for named address spaces. If the pointers point to
1250 different named addresses, then we must determine if one address space is a
1251 subset of the other. */
1252
1253 static int
1254 comp_target_types (location_t location, tree ttl, tree ttr)
1255 {
1256 int val;
1257 int val_ped;
1258 tree mvl = TREE_TYPE (ttl);
1259 tree mvr = TREE_TYPE (ttr);
1260 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1261 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1262 addr_space_t as_common;
1263 bool enum_and_int_p;
1264
1265 /* Fail if pointers point to incompatible address spaces. */
1266 if (!addr_space_superset (asl, asr, &as_common))
1267 return 0;
1268
1269 /* For pedantic record result of comptypes on arrays before losing
1270 qualifiers on the element type below. */
1271 val_ped = 1;
1272
1273 if (TREE_CODE (mvl) == ARRAY_TYPE
1274 && TREE_CODE (mvr) == ARRAY_TYPE)
1275 val_ped = comptypes (mvl, mvr);
1276
1277 /* Qualifiers on element types of array types that are
1278 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1279
1280 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1281 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1282 : TYPE_MAIN_VARIANT (mvl));
1283
1284 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1285 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1286 : TYPE_MAIN_VARIANT (mvr));
1287
1288 enum_and_int_p = false;
1289 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1290
1291 if (val == 1 && val_ped != 1)
1292 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1293 "are incompatible in ISO C");
1294
1295 if (val == 2)
1296 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1297
1298 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1299 warning_at (location, OPT_Wc___compat,
1300 "pointer target types incompatible in C++");
1301
1302 return val;
1303 }
1304 \f
1305 /* Subroutines of `comptypes'. */
1306
1307 /* Determine whether two trees derive from the same translation unit.
1308 If the CONTEXT chain ends in a null, that tree's context is still
1309 being parsed, so if two trees have context chains ending in null,
1310 they're in the same translation unit. */
1311
1312 bool
1313 same_translation_unit_p (const_tree t1, const_tree t2)
1314 {
1315 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1316 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1317 {
1318 case tcc_declaration:
1319 t1 = DECL_CONTEXT (t1); break;
1320 case tcc_type:
1321 t1 = TYPE_CONTEXT (t1); break;
1322 case tcc_exceptional:
1323 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1324 default: gcc_unreachable ();
1325 }
1326
1327 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1328 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1329 {
1330 case tcc_declaration:
1331 t2 = DECL_CONTEXT (t2); break;
1332 case tcc_type:
1333 t2 = TYPE_CONTEXT (t2); break;
1334 case tcc_exceptional:
1335 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1336 default: gcc_unreachable ();
1337 }
1338
1339 return t1 == t2;
1340 }
1341
1342 /* Allocate the seen two types, assuming that they are compatible. */
1343
1344 static struct tagged_tu_seen_cache *
1345 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1346 {
1347 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1348 tu->next = tagged_tu_seen_base;
1349 tu->t1 = t1;
1350 tu->t2 = t2;
1351
1352 tagged_tu_seen_base = tu;
1353
1354 /* The C standard says that two structures in different translation
1355 units are compatible with each other only if the types of their
1356 fields are compatible (among other things). We assume that they
1357 are compatible until proven otherwise when building the cache.
1358 An example where this can occur is:
1359 struct a
1360 {
1361 struct a *next;
1362 };
1363 If we are comparing this against a similar struct in another TU,
1364 and did not assume they were compatible, we end up with an infinite
1365 loop. */
1366 tu->val = 1;
1367 return tu;
1368 }
1369
1370 /* Free the seen types until we get to TU_TIL. */
1371
1372 static void
1373 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1374 {
1375 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1376 while (tu != tu_til)
1377 {
1378 const struct tagged_tu_seen_cache *const tu1
1379 = (const struct tagged_tu_seen_cache *) tu;
1380 tu = tu1->next;
1381 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1382 }
1383 tagged_tu_seen_base = tu_til;
1384 }
1385
1386 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1387 compatible. If the two types are not the same (which has been
1388 checked earlier), this can only happen when multiple translation
1389 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1390 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1391 comptypes_internal. */
1392
1393 static int
1394 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1395 bool *enum_and_int_p, bool *different_types_p)
1396 {
1397 tree s1, s2;
1398 bool needs_warning = false;
1399
1400 /* We have to verify that the tags of the types are the same. This
1401 is harder than it looks because this may be a typedef, so we have
1402 to go look at the original type. It may even be a typedef of a
1403 typedef...
1404 In the case of compiler-created builtin structs the TYPE_DECL
1405 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1406 while (TYPE_NAME (t1)
1407 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1408 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1409 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1410
1411 while (TYPE_NAME (t2)
1412 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1413 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1414 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1415
1416 /* C90 didn't have the requirement that the two tags be the same. */
1417 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1418 return 0;
1419
1420 /* C90 didn't say what happened if one or both of the types were
1421 incomplete; we choose to follow C99 rules here, which is that they
1422 are compatible. */
1423 if (TYPE_SIZE (t1) == NULL
1424 || TYPE_SIZE (t2) == NULL)
1425 return 1;
1426
1427 {
1428 const struct tagged_tu_seen_cache * tts_i;
1429 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1430 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1431 return tts_i->val;
1432 }
1433
1434 switch (TREE_CODE (t1))
1435 {
1436 case ENUMERAL_TYPE:
1437 {
1438 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1439 /* Speed up the case where the type values are in the same order. */
1440 tree tv1 = TYPE_VALUES (t1);
1441 tree tv2 = TYPE_VALUES (t2);
1442
1443 if (tv1 == tv2)
1444 {
1445 return 1;
1446 }
1447
1448 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1449 {
1450 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1451 break;
1452 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1453 {
1454 tu->val = 0;
1455 return 0;
1456 }
1457 }
1458
1459 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1460 {
1461 return 1;
1462 }
1463 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1464 {
1465 tu->val = 0;
1466 return 0;
1467 }
1468
1469 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1470 {
1471 tu->val = 0;
1472 return 0;
1473 }
1474
1475 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1476 {
1477 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1478 if (s2 == NULL
1479 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1480 {
1481 tu->val = 0;
1482 return 0;
1483 }
1484 }
1485 return 1;
1486 }
1487
1488 case UNION_TYPE:
1489 {
1490 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1491 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1492 {
1493 tu->val = 0;
1494 return 0;
1495 }
1496
1497 /* Speed up the common case where the fields are in the same order. */
1498 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1499 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1500 {
1501 int result;
1502
1503 if (DECL_NAME (s1) != DECL_NAME (s2))
1504 break;
1505 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1506 enum_and_int_p, different_types_p);
1507
1508 if (result != 1 && !DECL_NAME (s1))
1509 break;
1510 if (result == 0)
1511 {
1512 tu->val = 0;
1513 return 0;
1514 }
1515 if (result == 2)
1516 needs_warning = true;
1517
1518 if (TREE_CODE (s1) == FIELD_DECL
1519 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1520 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1521 {
1522 tu->val = 0;
1523 return 0;
1524 }
1525 }
1526 if (!s1 && !s2)
1527 {
1528 tu->val = needs_warning ? 2 : 1;
1529 return tu->val;
1530 }
1531
1532 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1533 {
1534 bool ok = false;
1535
1536 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1537 if (DECL_NAME (s1) == DECL_NAME (s2))
1538 {
1539 int result;
1540
1541 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1542 enum_and_int_p,
1543 different_types_p);
1544
1545 if (result != 1 && !DECL_NAME (s1))
1546 continue;
1547 if (result == 0)
1548 {
1549 tu->val = 0;
1550 return 0;
1551 }
1552 if (result == 2)
1553 needs_warning = true;
1554
1555 if (TREE_CODE (s1) == FIELD_DECL
1556 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1557 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1558 break;
1559
1560 ok = true;
1561 break;
1562 }
1563 if (!ok)
1564 {
1565 tu->val = 0;
1566 return 0;
1567 }
1568 }
1569 tu->val = needs_warning ? 2 : 10;
1570 return tu->val;
1571 }
1572
1573 case RECORD_TYPE:
1574 {
1575 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1576
1577 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1578 s1 && s2;
1579 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1580 {
1581 int result;
1582 if (TREE_CODE (s1) != TREE_CODE (s2)
1583 || DECL_NAME (s1) != DECL_NAME (s2))
1584 break;
1585 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1586 enum_and_int_p, different_types_p);
1587 if (result == 0)
1588 break;
1589 if (result == 2)
1590 needs_warning = true;
1591
1592 if (TREE_CODE (s1) == FIELD_DECL
1593 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1594 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1595 break;
1596 }
1597 if (s1 && s2)
1598 tu->val = 0;
1599 else
1600 tu->val = needs_warning ? 2 : 1;
1601 return tu->val;
1602 }
1603
1604 default:
1605 gcc_unreachable ();
1606 }
1607 }
1608
1609 /* Return 1 if two function types F1 and F2 are compatible.
1610 If either type specifies no argument types,
1611 the other must specify a fixed number of self-promoting arg types.
1612 Otherwise, if one type specifies only the number of arguments,
1613 the other must specify that number of self-promoting arg types.
1614 Otherwise, the argument types must match.
1615 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1616
1617 static int
1618 function_types_compatible_p (const_tree f1, const_tree f2,
1619 bool *enum_and_int_p, bool *different_types_p)
1620 {
1621 tree args1, args2;
1622 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1623 int val = 1;
1624 int val1;
1625 tree ret1, ret2;
1626
1627 ret1 = TREE_TYPE (f1);
1628 ret2 = TREE_TYPE (f2);
1629
1630 /* 'volatile' qualifiers on a function's return type used to mean
1631 the function is noreturn. */
1632 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1633 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1634 if (TYPE_VOLATILE (ret1))
1635 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1636 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1637 if (TYPE_VOLATILE (ret2))
1638 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1639 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1640 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1641 if (val == 0)
1642 return 0;
1643
1644 args1 = TYPE_ARG_TYPES (f1);
1645 args2 = TYPE_ARG_TYPES (f2);
1646
1647 if (different_types_p != NULL
1648 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1649 *different_types_p = true;
1650
1651 /* An unspecified parmlist matches any specified parmlist
1652 whose argument types don't need default promotions. */
1653
1654 if (args1 == NULL_TREE)
1655 {
1656 if (!self_promoting_args_p (args2))
1657 return 0;
1658 /* If one of these types comes from a non-prototype fn definition,
1659 compare that with the other type's arglist.
1660 If they don't match, ask for a warning (but no error). */
1661 if (TYPE_ACTUAL_ARG_TYPES (f1)
1662 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1663 enum_and_int_p, different_types_p))
1664 val = 2;
1665 return val;
1666 }
1667 if (args2 == NULL_TREE)
1668 {
1669 if (!self_promoting_args_p (args1))
1670 return 0;
1671 if (TYPE_ACTUAL_ARG_TYPES (f2)
1672 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1673 enum_and_int_p, different_types_p))
1674 val = 2;
1675 return val;
1676 }
1677
1678 /* Both types have argument lists: compare them and propagate results. */
1679 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1680 different_types_p);
1681 return val1 != 1 ? val1 : val;
1682 }
1683
1684 /* Check two lists of types for compatibility, returning 0 for
1685 incompatible, 1 for compatible, or 2 for compatible with
1686 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1687 comptypes_internal. */
1688
1689 static int
1690 type_lists_compatible_p (const_tree args1, const_tree args2,
1691 bool *enum_and_int_p, bool *different_types_p)
1692 {
1693 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1694 int val = 1;
1695 int newval = 0;
1696
1697 while (1)
1698 {
1699 tree a1, mv1, a2, mv2;
1700 if (args1 == NULL_TREE && args2 == NULL_TREE)
1701 return val;
1702 /* If one list is shorter than the other,
1703 they fail to match. */
1704 if (args1 == NULL_TREE || args2 == NULL_TREE)
1705 return 0;
1706 mv1 = a1 = TREE_VALUE (args1);
1707 mv2 = a2 = TREE_VALUE (args2);
1708 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1709 mv1 = (TYPE_ATOMIC (mv1)
1710 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1711 TYPE_QUAL_ATOMIC)
1712 : TYPE_MAIN_VARIANT (mv1));
1713 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1714 mv2 = (TYPE_ATOMIC (mv2)
1715 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1716 TYPE_QUAL_ATOMIC)
1717 : TYPE_MAIN_VARIANT (mv2));
1718 /* A null pointer instead of a type
1719 means there is supposed to be an argument
1720 but nothing is specified about what type it has.
1721 So match anything that self-promotes. */
1722 if (different_types_p != NULL
1723 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1724 *different_types_p = true;
1725 if (a1 == NULL_TREE)
1726 {
1727 if (c_type_promotes_to (a2) != a2)
1728 return 0;
1729 }
1730 else if (a2 == NULL_TREE)
1731 {
1732 if (c_type_promotes_to (a1) != a1)
1733 return 0;
1734 }
1735 /* If one of the lists has an error marker, ignore this arg. */
1736 else if (TREE_CODE (a1) == ERROR_MARK
1737 || TREE_CODE (a2) == ERROR_MARK)
1738 ;
1739 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1740 different_types_p)))
1741 {
1742 if (different_types_p != NULL)
1743 *different_types_p = true;
1744 /* Allow wait (union {union wait *u; int *i} *)
1745 and wait (union wait *) to be compatible. */
1746 if (TREE_CODE (a1) == UNION_TYPE
1747 && (TYPE_NAME (a1) == NULL_TREE
1748 || TYPE_TRANSPARENT_AGGR (a1))
1749 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1750 && tree_int_cst_equal (TYPE_SIZE (a1),
1751 TYPE_SIZE (a2)))
1752 {
1753 tree memb;
1754 for (memb = TYPE_FIELDS (a1);
1755 memb; memb = DECL_CHAIN (memb))
1756 {
1757 tree mv3 = TREE_TYPE (memb);
1758 if (mv3 && mv3 != error_mark_node
1759 && TREE_CODE (mv3) != ARRAY_TYPE)
1760 mv3 = (TYPE_ATOMIC (mv3)
1761 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1762 TYPE_QUAL_ATOMIC)
1763 : TYPE_MAIN_VARIANT (mv3));
1764 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1765 different_types_p))
1766 break;
1767 }
1768 if (memb == NULL_TREE)
1769 return 0;
1770 }
1771 else if (TREE_CODE (a2) == UNION_TYPE
1772 && (TYPE_NAME (a2) == NULL_TREE
1773 || TYPE_TRANSPARENT_AGGR (a2))
1774 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1775 && tree_int_cst_equal (TYPE_SIZE (a2),
1776 TYPE_SIZE (a1)))
1777 {
1778 tree memb;
1779 for (memb = TYPE_FIELDS (a2);
1780 memb; memb = DECL_CHAIN (memb))
1781 {
1782 tree mv3 = TREE_TYPE (memb);
1783 if (mv3 && mv3 != error_mark_node
1784 && TREE_CODE (mv3) != ARRAY_TYPE)
1785 mv3 = (TYPE_ATOMIC (mv3)
1786 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1787 TYPE_QUAL_ATOMIC)
1788 : TYPE_MAIN_VARIANT (mv3));
1789 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1790 different_types_p))
1791 break;
1792 }
1793 if (memb == NULL_TREE)
1794 return 0;
1795 }
1796 else
1797 return 0;
1798 }
1799
1800 /* comptypes said ok, but record if it said to warn. */
1801 if (newval > val)
1802 val = newval;
1803
1804 args1 = TREE_CHAIN (args1);
1805 args2 = TREE_CHAIN (args2);
1806 }
1807 }
1808 \f
1809 /* Compute the size to increment a pointer by. When a function type or void
1810 type or incomplete type is passed, size_one_node is returned.
1811 This function does not emit any diagnostics; the caller is responsible
1812 for that. */
1813
1814 static tree
1815 c_size_in_bytes (const_tree type)
1816 {
1817 enum tree_code code = TREE_CODE (type);
1818
1819 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1820 || !COMPLETE_TYPE_P (type))
1821 return size_one_node;
1822
1823 /* Convert in case a char is more than one unit. */
1824 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1825 size_int (TYPE_PRECISION (char_type_node)
1826 / BITS_PER_UNIT));
1827 }
1828 \f
1829 /* Return either DECL or its known constant value (if it has one). */
1830
1831 tree
1832 decl_constant_value (tree decl)
1833 {
1834 if (/* Don't change a variable array bound or initial value to a constant
1835 in a place where a variable is invalid. Note that DECL_INITIAL
1836 isn't valid for a PARM_DECL. */
1837 current_function_decl != NULL_TREE
1838 && TREE_CODE (decl) != PARM_DECL
1839 && !TREE_THIS_VOLATILE (decl)
1840 && TREE_READONLY (decl)
1841 && DECL_INITIAL (decl) != NULL_TREE
1842 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1843 /* This is invalid if initial value is not constant.
1844 If it has either a function call, a memory reference,
1845 or a variable, then re-evaluating it could give different results. */
1846 && TREE_CONSTANT (DECL_INITIAL (decl))
1847 /* Check for cases where this is sub-optimal, even though valid. */
1848 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1849 return DECL_INITIAL (decl);
1850 return decl;
1851 }
1852
1853 /* Convert the array expression EXP to a pointer. */
1854 static tree
1855 array_to_pointer_conversion (location_t loc, tree exp)
1856 {
1857 tree orig_exp = exp;
1858 tree type = TREE_TYPE (exp);
1859 tree adr;
1860 tree restype = TREE_TYPE (type);
1861 tree ptrtype;
1862
1863 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1864
1865 STRIP_TYPE_NOPS (exp);
1866
1867 if (TREE_NO_WARNING (orig_exp))
1868 TREE_NO_WARNING (exp) = 1;
1869
1870 ptrtype = build_pointer_type (restype);
1871
1872 if (INDIRECT_REF_P (exp))
1873 return convert (ptrtype, TREE_OPERAND (exp, 0));
1874
1875 /* In C++ array compound literals are temporary objects unless they are
1876 const or appear in namespace scope, so they are destroyed too soon
1877 to use them for much of anything (c++/53220). */
1878 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1879 {
1880 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1881 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1882 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1883 "converting an array compound literal to a pointer "
1884 "is ill-formed in C++");
1885 }
1886
1887 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1888 return convert (ptrtype, adr);
1889 }
1890
1891 /* Convert the function expression EXP to a pointer. */
1892 static tree
1893 function_to_pointer_conversion (location_t loc, tree exp)
1894 {
1895 tree orig_exp = exp;
1896
1897 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1898
1899 STRIP_TYPE_NOPS (exp);
1900
1901 if (TREE_NO_WARNING (orig_exp))
1902 TREE_NO_WARNING (exp) = 1;
1903
1904 return build_unary_op (loc, ADDR_EXPR, exp, false);
1905 }
1906
1907 /* Mark EXP as read, not just set, for set but not used -Wunused
1908 warning purposes. */
1909
1910 void
1911 mark_exp_read (tree exp)
1912 {
1913 switch (TREE_CODE (exp))
1914 {
1915 case VAR_DECL:
1916 case PARM_DECL:
1917 DECL_READ_P (exp) = 1;
1918 break;
1919 case ARRAY_REF:
1920 case COMPONENT_REF:
1921 case MODIFY_EXPR:
1922 case REALPART_EXPR:
1923 case IMAGPART_EXPR:
1924 CASE_CONVERT:
1925 case ADDR_EXPR:
1926 case VIEW_CONVERT_EXPR:
1927 mark_exp_read (TREE_OPERAND (exp, 0));
1928 break;
1929 case COMPOUND_EXPR:
1930 case C_MAYBE_CONST_EXPR:
1931 mark_exp_read (TREE_OPERAND (exp, 1));
1932 break;
1933 default:
1934 break;
1935 }
1936 }
1937
1938 /* Perform the default conversion of arrays and functions to pointers.
1939 Return the result of converting EXP. For any other expression, just
1940 return EXP.
1941
1942 LOC is the location of the expression. */
1943
1944 struct c_expr
1945 default_function_array_conversion (location_t loc, struct c_expr exp)
1946 {
1947 tree orig_exp = exp.value;
1948 tree type = TREE_TYPE (exp.value);
1949 enum tree_code code = TREE_CODE (type);
1950
1951 switch (code)
1952 {
1953 case ARRAY_TYPE:
1954 {
1955 bool not_lvalue = false;
1956 bool lvalue_array_p;
1957
1958 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1959 || CONVERT_EXPR_P (exp.value))
1960 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1961 {
1962 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1963 not_lvalue = true;
1964 exp.value = TREE_OPERAND (exp.value, 0);
1965 }
1966
1967 if (TREE_NO_WARNING (orig_exp))
1968 TREE_NO_WARNING (exp.value) = 1;
1969
1970 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1971 if (!flag_isoc99 && !lvalue_array_p)
1972 {
1973 /* Before C99, non-lvalue arrays do not decay to pointers.
1974 Normally, using such an array would be invalid; but it can
1975 be used correctly inside sizeof or as a statement expression.
1976 Thus, do not give an error here; an error will result later. */
1977 return exp;
1978 }
1979
1980 exp.value = array_to_pointer_conversion (loc, exp.value);
1981 }
1982 break;
1983 case FUNCTION_TYPE:
1984 exp.value = function_to_pointer_conversion (loc, exp.value);
1985 break;
1986 default:
1987 break;
1988 }
1989
1990 return exp;
1991 }
1992
1993 struct c_expr
1994 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1995 {
1996 mark_exp_read (exp.value);
1997 return default_function_array_conversion (loc, exp);
1998 }
1999
2000 /* Return whether EXPR should be treated as an atomic lvalue for the
2001 purposes of load and store handling. */
2002
2003 static bool
2004 really_atomic_lvalue (tree expr)
2005 {
2006 if (error_operand_p (expr))
2007 return false;
2008 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2009 return false;
2010 if (!lvalue_p (expr))
2011 return false;
2012
2013 /* Ignore _Atomic on register variables, since their addresses can't
2014 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2015 sequences wouldn't work. Ignore _Atomic on structures containing
2016 bit-fields, since accessing elements of atomic structures or
2017 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2018 it's undefined at translation time or execution time, and the
2019 normal atomic sequences again wouldn't work. */
2020 while (handled_component_p (expr))
2021 {
2022 if (TREE_CODE (expr) == COMPONENT_REF
2023 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2024 return false;
2025 expr = TREE_OPERAND (expr, 0);
2026 }
2027 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2028 return false;
2029 return true;
2030 }
2031
2032 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2033 including converting functions and arrays to pointers if CONVERT_P.
2034 If READ_P, also mark the expression as having been read. */
2035
2036 struct c_expr
2037 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2038 bool convert_p, bool read_p)
2039 {
2040 if (read_p)
2041 mark_exp_read (exp.value);
2042 if (convert_p)
2043 exp = default_function_array_conversion (loc, exp);
2044 if (really_atomic_lvalue (exp.value))
2045 {
2046 vec<tree, va_gc> *params;
2047 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2048 tree expr_type = TREE_TYPE (exp.value);
2049 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2050 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2051
2052 gcc_assert (TYPE_ATOMIC (expr_type));
2053
2054 /* Expansion of a generic atomic load may require an addition
2055 element, so allocate enough to prevent a resize. */
2056 vec_alloc (params, 4);
2057
2058 /* Remove the qualifiers for the rest of the expressions and
2059 create the VAL temp variable to hold the RHS. */
2060 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2061 tmp = create_tmp_var_raw (nonatomic_type);
2062 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2063 TREE_ADDRESSABLE (tmp) = 1;
2064 TREE_NO_WARNING (tmp) = 1;
2065
2066 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2067 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2068 params->quick_push (expr_addr);
2069 params->quick_push (tmp_addr);
2070 params->quick_push (seq_cst);
2071 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2072
2073 /* EXPR is always read. */
2074 mark_exp_read (exp.value);
2075
2076 /* Return tmp which contains the value loaded. */
2077 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2078 NULL_TREE, NULL_TREE);
2079 }
2080 return exp;
2081 }
2082
2083 /* EXP is an expression of integer type. Apply the integer promotions
2084 to it and return the promoted value. */
2085
2086 tree
2087 perform_integral_promotions (tree exp)
2088 {
2089 tree type = TREE_TYPE (exp);
2090 enum tree_code code = TREE_CODE (type);
2091
2092 gcc_assert (INTEGRAL_TYPE_P (type));
2093
2094 /* Normally convert enums to int,
2095 but convert wide enums to something wider. */
2096 if (code == ENUMERAL_TYPE)
2097 {
2098 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2099 TYPE_PRECISION (integer_type_node)),
2100 ((TYPE_PRECISION (type)
2101 >= TYPE_PRECISION (integer_type_node))
2102 && TYPE_UNSIGNED (type)));
2103
2104 return convert (type, exp);
2105 }
2106
2107 /* ??? This should no longer be needed now bit-fields have their
2108 proper types. */
2109 if (TREE_CODE (exp) == COMPONENT_REF
2110 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2111 /* If it's thinner than an int, promote it like a
2112 c_promoting_integer_type_p, otherwise leave it alone. */
2113 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2114 TYPE_PRECISION (integer_type_node)))
2115 return convert (integer_type_node, exp);
2116
2117 if (c_promoting_integer_type_p (type))
2118 {
2119 /* Preserve unsignedness if not really getting any wider. */
2120 if (TYPE_UNSIGNED (type)
2121 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2122 return convert (unsigned_type_node, exp);
2123
2124 return convert (integer_type_node, exp);
2125 }
2126
2127 return exp;
2128 }
2129
2130
2131 /* Perform default promotions for C data used in expressions.
2132 Enumeral types or short or char are converted to int.
2133 In addition, manifest constants symbols are replaced by their values. */
2134
2135 tree
2136 default_conversion (tree exp)
2137 {
2138 tree orig_exp;
2139 tree type = TREE_TYPE (exp);
2140 enum tree_code code = TREE_CODE (type);
2141 tree promoted_type;
2142
2143 mark_exp_read (exp);
2144
2145 /* Functions and arrays have been converted during parsing. */
2146 gcc_assert (code != FUNCTION_TYPE);
2147 if (code == ARRAY_TYPE)
2148 return exp;
2149
2150 /* Constants can be used directly unless they're not loadable. */
2151 if (TREE_CODE (exp) == CONST_DECL)
2152 exp = DECL_INITIAL (exp);
2153
2154 /* Strip no-op conversions. */
2155 orig_exp = exp;
2156 STRIP_TYPE_NOPS (exp);
2157
2158 if (TREE_NO_WARNING (orig_exp))
2159 TREE_NO_WARNING (exp) = 1;
2160
2161 if (code == VOID_TYPE)
2162 {
2163 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2164 "void value not ignored as it ought to be");
2165 return error_mark_node;
2166 }
2167
2168 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2169 if (exp == error_mark_node)
2170 return error_mark_node;
2171
2172 promoted_type = targetm.promoted_type (type);
2173 if (promoted_type)
2174 return convert (promoted_type, exp);
2175
2176 if (INTEGRAL_TYPE_P (type))
2177 return perform_integral_promotions (exp);
2178
2179 return exp;
2180 }
2181 \f
2182 /* Look up COMPONENT in a structure or union TYPE.
2183
2184 If the component name is not found, returns NULL_TREE. Otherwise,
2185 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2186 stepping down the chain to the component, which is in the last
2187 TREE_VALUE of the list. Normally the list is of length one, but if
2188 the component is embedded within (nested) anonymous structures or
2189 unions, the list steps down the chain to the component. */
2190
2191 static tree
2192 lookup_field (tree type, tree component)
2193 {
2194 tree field;
2195
2196 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2197 to the field elements. Use a binary search on this array to quickly
2198 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2199 will always be set for structures which have many elements. */
2200
2201 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2202 {
2203 int bot, top, half;
2204 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2205
2206 field = TYPE_FIELDS (type);
2207 bot = 0;
2208 top = TYPE_LANG_SPECIFIC (type)->s->len;
2209 while (top - bot > 1)
2210 {
2211 half = (top - bot + 1) >> 1;
2212 field = field_array[bot+half];
2213
2214 if (DECL_NAME (field) == NULL_TREE)
2215 {
2216 /* Step through all anon unions in linear fashion. */
2217 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2218 {
2219 field = field_array[bot++];
2220 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2221 {
2222 tree anon = lookup_field (TREE_TYPE (field), component);
2223
2224 if (anon)
2225 return tree_cons (NULL_TREE, field, anon);
2226
2227 /* The Plan 9 compiler permits referring
2228 directly to an anonymous struct/union field
2229 using a typedef name. */
2230 if (flag_plan9_extensions
2231 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2232 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2233 == TYPE_DECL)
2234 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2235 == component))
2236 break;
2237 }
2238 }
2239
2240 /* Entire record is only anon unions. */
2241 if (bot > top)
2242 return NULL_TREE;
2243
2244 /* Restart the binary search, with new lower bound. */
2245 continue;
2246 }
2247
2248 if (DECL_NAME (field) == component)
2249 break;
2250 if (DECL_NAME (field) < component)
2251 bot += half;
2252 else
2253 top = bot + half;
2254 }
2255
2256 if (DECL_NAME (field_array[bot]) == component)
2257 field = field_array[bot];
2258 else if (DECL_NAME (field) != component)
2259 return NULL_TREE;
2260 }
2261 else
2262 {
2263 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2264 {
2265 if (DECL_NAME (field) == NULL_TREE
2266 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2267 {
2268 tree anon = lookup_field (TREE_TYPE (field), component);
2269
2270 if (anon)
2271 return tree_cons (NULL_TREE, field, anon);
2272
2273 /* The Plan 9 compiler permits referring directly to an
2274 anonymous struct/union field using a typedef
2275 name. */
2276 if (flag_plan9_extensions
2277 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2278 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2279 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2280 == component))
2281 break;
2282 }
2283
2284 if (DECL_NAME (field) == component)
2285 break;
2286 }
2287
2288 if (field == NULL_TREE)
2289 return NULL_TREE;
2290 }
2291
2292 return tree_cons (NULL_TREE, field, NULL_TREE);
2293 }
2294
2295 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2296
2297 static void
2298 lookup_field_fuzzy_find_candidates (tree type, tree component,
2299 vec<tree> *candidates)
2300 {
2301 tree field;
2302 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2303 {
2304 if (DECL_NAME (field) == NULL_TREE
2305 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2306 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2307 candidates);
2308
2309 if (DECL_NAME (field))
2310 candidates->safe_push (DECL_NAME (field));
2311 }
2312 }
2313
2314 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2315 rather than returning a TREE_LIST for an exact match. */
2316
2317 static tree
2318 lookup_field_fuzzy (tree type, tree component)
2319 {
2320 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2321
2322 /* First, gather a list of candidates. */
2323 auto_vec <tree> candidates;
2324
2325 lookup_field_fuzzy_find_candidates (type, component,
2326 &candidates);
2327
2328 return find_closest_identifier (component, &candidates);
2329 }
2330
2331 /* Support function for build_component_ref's error-handling.
2332
2333 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2334 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2335
2336 static bool
2337 should_suggest_deref_p (tree datum_type)
2338 {
2339 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2340 allows "." for ptrs; we could be handling a failed attempt
2341 to access a property. */
2342 if (c_dialect_objc ())
2343 return false;
2344
2345 /* Only suggest it for pointers... */
2346 if (TREE_CODE (datum_type) != POINTER_TYPE)
2347 return false;
2348
2349 /* ...to structs/unions. */
2350 tree underlying_type = TREE_TYPE (datum_type);
2351 enum tree_code code = TREE_CODE (underlying_type);
2352 if (code == RECORD_TYPE || code == UNION_TYPE)
2353 return true;
2354 else
2355 return false;
2356 }
2357
2358 /* Make an expression to refer to the COMPONENT field of structure or
2359 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2360 location of the COMPONENT_REF. COMPONENT_LOC is the location
2361 of COMPONENT. */
2362
2363 tree
2364 build_component_ref (location_t loc, tree datum, tree component,
2365 location_t component_loc)
2366 {
2367 tree type = TREE_TYPE (datum);
2368 enum tree_code code = TREE_CODE (type);
2369 tree field = NULL;
2370 tree ref;
2371 bool datum_lvalue = lvalue_p (datum);
2372
2373 if (!objc_is_public (datum, component))
2374 return error_mark_node;
2375
2376 /* Detect Objective-C property syntax object.property. */
2377 if (c_dialect_objc ()
2378 && (ref = objc_maybe_build_component_ref (datum, component)))
2379 return ref;
2380
2381 /* See if there is a field or component with name COMPONENT. */
2382
2383 if (code == RECORD_TYPE || code == UNION_TYPE)
2384 {
2385 if (!COMPLETE_TYPE_P (type))
2386 {
2387 c_incomplete_type_error (loc, NULL_TREE, type);
2388 return error_mark_node;
2389 }
2390
2391 field = lookup_field (type, component);
2392
2393 if (!field)
2394 {
2395 tree guessed_id = lookup_field_fuzzy (type, component);
2396 if (guessed_id)
2397 {
2398 /* Attempt to provide a fixit replacement hint, if
2399 we have a valid range for the component. */
2400 location_t reported_loc
2401 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2402 gcc_rich_location rich_loc (reported_loc);
2403 if (component_loc != UNKNOWN_LOCATION)
2404 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2405 error_at_rich_loc
2406 (&rich_loc,
2407 "%qT has no member named %qE; did you mean %qE?",
2408 type, component, guessed_id);
2409 }
2410 else
2411 error_at (loc, "%qT has no member named %qE", type, component);
2412 return error_mark_node;
2413 }
2414
2415 /* Accessing elements of atomic structures or unions is undefined
2416 behavior (C11 6.5.2.3#5). */
2417 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2418 {
2419 if (code == RECORD_TYPE)
2420 warning_at (loc, 0, "accessing a member %qE of an atomic "
2421 "structure %qE", component, datum);
2422 else
2423 warning_at (loc, 0, "accessing a member %qE of an atomic "
2424 "union %qE", component, datum);
2425 }
2426
2427 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2428 This might be better solved in future the way the C++ front
2429 end does it - by giving the anonymous entities each a
2430 separate name and type, and then have build_component_ref
2431 recursively call itself. We can't do that here. */
2432 do
2433 {
2434 tree subdatum = TREE_VALUE (field);
2435 int quals;
2436 tree subtype;
2437 bool use_datum_quals;
2438
2439 if (TREE_TYPE (subdatum) == error_mark_node)
2440 return error_mark_node;
2441
2442 /* If this is an rvalue, it does not have qualifiers in C
2443 standard terms and we must avoid propagating such
2444 qualifiers down to a non-lvalue array that is then
2445 converted to a pointer. */
2446 use_datum_quals = (datum_lvalue
2447 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2448
2449 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2450 if (use_datum_quals)
2451 quals |= TYPE_QUALS (TREE_TYPE (datum));
2452 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2453
2454 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2455 NULL_TREE);
2456 SET_EXPR_LOCATION (ref, loc);
2457 if (TREE_READONLY (subdatum)
2458 || (use_datum_quals && TREE_READONLY (datum)))
2459 TREE_READONLY (ref) = 1;
2460 if (TREE_THIS_VOLATILE (subdatum)
2461 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2462 TREE_THIS_VOLATILE (ref) = 1;
2463
2464 if (TREE_DEPRECATED (subdatum))
2465 warn_deprecated_use (subdatum, NULL_TREE);
2466
2467 datum = ref;
2468
2469 field = TREE_CHAIN (field);
2470 }
2471 while (field);
2472
2473 return ref;
2474 }
2475 else if (should_suggest_deref_p (type))
2476 {
2477 /* Special-case the error message for "ptr.field" for the case
2478 where the user has confused "." vs "->". */
2479 rich_location richloc (line_table, loc);
2480 /* "loc" should be the "." token. */
2481 richloc.add_fixit_replace ("->");
2482 error_at_rich_loc (&richloc,
2483 "%qE is a pointer; did you mean to use %<->%>?",
2484 datum);
2485 return error_mark_node;
2486 }
2487 else if (code != ERROR_MARK)
2488 error_at (loc,
2489 "request for member %qE in something not a structure or union",
2490 component);
2491
2492 return error_mark_node;
2493 }
2494 \f
2495 /* Given an expression PTR for a pointer, return an expression
2496 for the value pointed to.
2497 ERRORSTRING is the name of the operator to appear in error messages.
2498
2499 LOC is the location to use for the generated tree. */
2500
2501 tree
2502 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2503 {
2504 tree pointer = default_conversion (ptr);
2505 tree type = TREE_TYPE (pointer);
2506 tree ref;
2507
2508 if (TREE_CODE (type) == POINTER_TYPE)
2509 {
2510 if (CONVERT_EXPR_P (pointer)
2511 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2512 {
2513 /* If a warning is issued, mark it to avoid duplicates from
2514 the backend. This only needs to be done at
2515 warn_strict_aliasing > 2. */
2516 if (warn_strict_aliasing > 2)
2517 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2518 type, TREE_OPERAND (pointer, 0)))
2519 TREE_NO_WARNING (pointer) = 1;
2520 }
2521
2522 if (TREE_CODE (pointer) == ADDR_EXPR
2523 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2524 == TREE_TYPE (type)))
2525 {
2526 ref = TREE_OPERAND (pointer, 0);
2527 protected_set_expr_location (ref, loc);
2528 return ref;
2529 }
2530 else
2531 {
2532 tree t = TREE_TYPE (type);
2533
2534 ref = build1 (INDIRECT_REF, t, pointer);
2535
2536 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2537 {
2538 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2539 {
2540 error_at (loc, "dereferencing pointer to incomplete type "
2541 "%qT", t);
2542 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2543 }
2544 return error_mark_node;
2545 }
2546 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2547 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2548
2549 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2550 so that we get the proper error message if the result is used
2551 to assign to. Also, &* is supposed to be a no-op.
2552 And ANSI C seems to specify that the type of the result
2553 should be the const type. */
2554 /* A de-reference of a pointer to const is not a const. It is valid
2555 to change it via some other pointer. */
2556 TREE_READONLY (ref) = TYPE_READONLY (t);
2557 TREE_SIDE_EFFECTS (ref)
2558 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2559 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2560 protected_set_expr_location (ref, loc);
2561 return ref;
2562 }
2563 }
2564 else if (TREE_CODE (pointer) != ERROR_MARK)
2565 invalid_indirection_error (loc, type, errstring);
2566
2567 return error_mark_node;
2568 }
2569
2570 /* This handles expressions of the form "a[i]", which denotes
2571 an array reference.
2572
2573 This is logically equivalent in C to *(a+i), but we may do it differently.
2574 If A is a variable or a member, we generate a primitive ARRAY_REF.
2575 This avoids forcing the array out of registers, and can work on
2576 arrays that are not lvalues (for example, members of structures returned
2577 by functions).
2578
2579 For vector types, allow vector[i] but not i[vector], and create
2580 *(((type*)&vectortype) + i) for the expression.
2581
2582 LOC is the location to use for the returned expression. */
2583
2584 tree
2585 build_array_ref (location_t loc, tree array, tree index)
2586 {
2587 tree ret;
2588 bool swapped = false;
2589 if (TREE_TYPE (array) == error_mark_node
2590 || TREE_TYPE (index) == error_mark_node)
2591 return error_mark_node;
2592
2593 if (flag_cilkplus && contains_array_notation_expr (index))
2594 {
2595 size_t rank = 0;
2596 if (!find_rank (loc, index, index, true, &rank))
2597 return error_mark_node;
2598 if (rank > 1)
2599 {
2600 error_at (loc, "rank of the array's index is greater than 1");
2601 return error_mark_node;
2602 }
2603 }
2604 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2605 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2606 /* Allow vector[index] but not index[vector]. */
2607 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2608 {
2609 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2610 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2611 {
2612 error_at (loc,
2613 "subscripted value is neither array nor pointer nor vector");
2614
2615 return error_mark_node;
2616 }
2617 std::swap (array, index);
2618 swapped = true;
2619 }
2620
2621 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2622 {
2623 error_at (loc, "array subscript is not an integer");
2624 return error_mark_node;
2625 }
2626
2627 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2628 {
2629 error_at (loc, "subscripted value is pointer to function");
2630 return error_mark_node;
2631 }
2632
2633 /* ??? Existing practice has been to warn only when the char
2634 index is syntactically the index, not for char[array]. */
2635 if (!swapped)
2636 warn_array_subscript_with_type_char (loc, index);
2637
2638 /* Apply default promotions *after* noticing character types. */
2639 index = default_conversion (index);
2640 if (index == error_mark_node)
2641 return error_mark_node;
2642
2643 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2644
2645 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2646 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2647
2648 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2649 {
2650 tree rval, type;
2651
2652 /* An array that is indexed by a non-constant
2653 cannot be stored in a register; we must be able to do
2654 address arithmetic on its address.
2655 Likewise an array of elements of variable size. */
2656 if (TREE_CODE (index) != INTEGER_CST
2657 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2658 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2659 {
2660 if (!c_mark_addressable (array, true))
2661 return error_mark_node;
2662 }
2663 /* An array that is indexed by a constant value which is not within
2664 the array bounds cannot be stored in a register either; because we
2665 would get a crash in store_bit_field/extract_bit_field when trying
2666 to access a non-existent part of the register. */
2667 if (TREE_CODE (index) == INTEGER_CST
2668 && TYPE_DOMAIN (TREE_TYPE (array))
2669 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2670 {
2671 if (!c_mark_addressable (array))
2672 return error_mark_node;
2673 }
2674
2675 if ((pedantic || warn_c90_c99_compat)
2676 && ! was_vector)
2677 {
2678 tree foo = array;
2679 while (TREE_CODE (foo) == COMPONENT_REF)
2680 foo = TREE_OPERAND (foo, 0);
2681 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2682 pedwarn (loc, OPT_Wpedantic,
2683 "ISO C forbids subscripting %<register%> array");
2684 else if (!lvalue_p (foo))
2685 pedwarn_c90 (loc, OPT_Wpedantic,
2686 "ISO C90 forbids subscripting non-lvalue "
2687 "array");
2688 }
2689
2690 type = TREE_TYPE (TREE_TYPE (array));
2691 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2692 /* Array ref is const/volatile if the array elements are
2693 or if the array is. */
2694 TREE_READONLY (rval)
2695 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2696 | TREE_READONLY (array));
2697 TREE_SIDE_EFFECTS (rval)
2698 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2699 | TREE_SIDE_EFFECTS (array));
2700 TREE_THIS_VOLATILE (rval)
2701 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2702 /* This was added by rms on 16 Nov 91.
2703 It fixes vol struct foo *a; a->elts[1]
2704 in an inline function.
2705 Hope it doesn't break something else. */
2706 | TREE_THIS_VOLATILE (array));
2707 ret = require_complete_type (loc, rval);
2708 protected_set_expr_location (ret, loc);
2709 if (non_lvalue)
2710 ret = non_lvalue_loc (loc, ret);
2711 return ret;
2712 }
2713 else
2714 {
2715 tree ar = default_conversion (array);
2716
2717 if (ar == error_mark_node)
2718 return ar;
2719
2720 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2721 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2722
2723 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2724 index, 0),
2725 RO_ARRAY_INDEXING);
2726 if (non_lvalue)
2727 ret = non_lvalue_loc (loc, ret);
2728 return ret;
2729 }
2730 }
2731 \f
2732 /* Build an external reference to identifier ID. FUN indicates
2733 whether this will be used for a function call. LOC is the source
2734 location of the identifier. This sets *TYPE to the type of the
2735 identifier, which is not the same as the type of the returned value
2736 for CONST_DECLs defined as enum constants. If the type of the
2737 identifier is not available, *TYPE is set to NULL. */
2738 tree
2739 build_external_ref (location_t loc, tree id, int fun, tree *type)
2740 {
2741 tree ref;
2742 tree decl = lookup_name (id);
2743
2744 /* In Objective-C, an instance variable (ivar) may be preferred to
2745 whatever lookup_name() found. */
2746 decl = objc_lookup_ivar (decl, id);
2747
2748 *type = NULL;
2749 if (decl && decl != error_mark_node)
2750 {
2751 ref = decl;
2752 *type = TREE_TYPE (ref);
2753 }
2754 else if (fun)
2755 /* Implicit function declaration. */
2756 ref = implicitly_declare (loc, id);
2757 else if (decl == error_mark_node)
2758 /* Don't complain about something that's already been
2759 complained about. */
2760 return error_mark_node;
2761 else
2762 {
2763 undeclared_variable (loc, id);
2764 return error_mark_node;
2765 }
2766
2767 if (TREE_TYPE (ref) == error_mark_node)
2768 return error_mark_node;
2769
2770 if (TREE_DEPRECATED (ref))
2771 warn_deprecated_use (ref, NULL_TREE);
2772
2773 /* Recursive call does not count as usage. */
2774 if (ref != current_function_decl)
2775 {
2776 TREE_USED (ref) = 1;
2777 }
2778
2779 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2780 {
2781 if (!in_sizeof && !in_typeof)
2782 C_DECL_USED (ref) = 1;
2783 else if (DECL_INITIAL (ref) == NULL_TREE
2784 && DECL_EXTERNAL (ref)
2785 && !TREE_PUBLIC (ref))
2786 record_maybe_used_decl (ref);
2787 }
2788
2789 if (TREE_CODE (ref) == CONST_DECL)
2790 {
2791 used_types_insert (TREE_TYPE (ref));
2792
2793 if (warn_cxx_compat
2794 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2795 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2796 {
2797 warning_at (loc, OPT_Wc___compat,
2798 ("enum constant defined in struct or union "
2799 "is not visible in C++"));
2800 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2801 }
2802
2803 ref = DECL_INITIAL (ref);
2804 TREE_CONSTANT (ref) = 1;
2805 }
2806 else if (current_function_decl != NULL_TREE
2807 && !DECL_FILE_SCOPE_P (current_function_decl)
2808 && (VAR_OR_FUNCTION_DECL_P (ref)
2809 || TREE_CODE (ref) == PARM_DECL))
2810 {
2811 tree context = decl_function_context (ref);
2812
2813 if (context != NULL_TREE && context != current_function_decl)
2814 DECL_NONLOCAL (ref) = 1;
2815 }
2816 /* C99 6.7.4p3: An inline definition of a function with external
2817 linkage ... shall not contain a reference to an identifier with
2818 internal linkage. */
2819 else if (current_function_decl != NULL_TREE
2820 && DECL_DECLARED_INLINE_P (current_function_decl)
2821 && DECL_EXTERNAL (current_function_decl)
2822 && VAR_OR_FUNCTION_DECL_P (ref)
2823 && (!VAR_P (ref) || TREE_STATIC (ref))
2824 && ! TREE_PUBLIC (ref)
2825 && DECL_CONTEXT (ref) != current_function_decl)
2826 record_inline_static (loc, current_function_decl, ref,
2827 csi_internal);
2828
2829 return ref;
2830 }
2831
2832 /* Record details of decls possibly used inside sizeof or typeof. */
2833 struct maybe_used_decl
2834 {
2835 /* The decl. */
2836 tree decl;
2837 /* The level seen at (in_sizeof + in_typeof). */
2838 int level;
2839 /* The next one at this level or above, or NULL. */
2840 struct maybe_used_decl *next;
2841 };
2842
2843 static struct maybe_used_decl *maybe_used_decls;
2844
2845 /* Record that DECL, an undefined static function reference seen
2846 inside sizeof or typeof, might be used if the operand of sizeof is
2847 a VLA type or the operand of typeof is a variably modified
2848 type. */
2849
2850 static void
2851 record_maybe_used_decl (tree decl)
2852 {
2853 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2854 t->decl = decl;
2855 t->level = in_sizeof + in_typeof;
2856 t->next = maybe_used_decls;
2857 maybe_used_decls = t;
2858 }
2859
2860 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2861 USED is false, just discard them. If it is true, mark them used
2862 (if no longer inside sizeof or typeof) or move them to the next
2863 level up (if still inside sizeof or typeof). */
2864
2865 void
2866 pop_maybe_used (bool used)
2867 {
2868 struct maybe_used_decl *p = maybe_used_decls;
2869 int cur_level = in_sizeof + in_typeof;
2870 while (p && p->level > cur_level)
2871 {
2872 if (used)
2873 {
2874 if (cur_level == 0)
2875 C_DECL_USED (p->decl) = 1;
2876 else
2877 p->level = cur_level;
2878 }
2879 p = p->next;
2880 }
2881 if (!used || cur_level == 0)
2882 maybe_used_decls = p;
2883 }
2884
2885 /* Return the result of sizeof applied to EXPR. */
2886
2887 struct c_expr
2888 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2889 {
2890 struct c_expr ret;
2891 if (expr.value == error_mark_node)
2892 {
2893 ret.value = error_mark_node;
2894 ret.original_code = ERROR_MARK;
2895 ret.original_type = NULL;
2896 pop_maybe_used (false);
2897 }
2898 else
2899 {
2900 bool expr_const_operands = true;
2901
2902 if (TREE_CODE (expr.value) == PARM_DECL
2903 && C_ARRAY_PARAMETER (expr.value))
2904 {
2905 if (warning_at (loc, OPT_Wsizeof_array_argument,
2906 "%<sizeof%> on array function parameter %qE will "
2907 "return size of %qT", expr.value,
2908 expr.original_type))
2909 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2910 }
2911 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2912 &expr_const_operands);
2913 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2914 c_last_sizeof_arg = expr.value;
2915 c_last_sizeof_loc = loc;
2916 ret.original_code = SIZEOF_EXPR;
2917 ret.original_type = NULL;
2918 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2919 {
2920 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2921 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2922 folded_expr, ret.value);
2923 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2924 SET_EXPR_LOCATION (ret.value, loc);
2925 }
2926 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2927 }
2928 return ret;
2929 }
2930
2931 /* Return the result of sizeof applied to T, a structure for the type
2932 name passed to sizeof (rather than the type itself). LOC is the
2933 location of the original expression. */
2934
2935 struct c_expr
2936 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2937 {
2938 tree type;
2939 struct c_expr ret;
2940 tree type_expr = NULL_TREE;
2941 bool type_expr_const = true;
2942 type = groktypename (t, &type_expr, &type_expr_const);
2943 ret.value = c_sizeof (loc, type);
2944 c_last_sizeof_arg = type;
2945 c_last_sizeof_loc = loc;
2946 ret.original_code = SIZEOF_EXPR;
2947 ret.original_type = NULL;
2948 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2949 && c_vla_type_p (type))
2950 {
2951 /* If the type is a [*] array, it is a VLA but is represented as
2952 having a size of zero. In such a case we must ensure that
2953 the result of sizeof does not get folded to a constant by
2954 c_fully_fold, because if the size is evaluated the result is
2955 not constant and so constraints on zero or negative size
2956 arrays must not be applied when this sizeof call is inside
2957 another array declarator. */
2958 if (!type_expr)
2959 type_expr = integer_zero_node;
2960 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2961 type_expr, ret.value);
2962 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2963 }
2964 pop_maybe_used (type != error_mark_node
2965 ? C_TYPE_VARIABLE_SIZE (type) : false);
2966 return ret;
2967 }
2968
2969 /* Build a function call to function FUNCTION with parameters PARAMS.
2970 The function call is at LOC.
2971 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2972 TREE_VALUE of each node is a parameter-expression.
2973 FUNCTION's data type may be a function type or a pointer-to-function. */
2974
2975 tree
2976 build_function_call (location_t loc, tree function, tree params)
2977 {
2978 vec<tree, va_gc> *v;
2979 tree ret;
2980
2981 vec_alloc (v, list_length (params));
2982 for (; params; params = TREE_CHAIN (params))
2983 v->quick_push (TREE_VALUE (params));
2984 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2985 vec_free (v);
2986 return ret;
2987 }
2988
2989 /* Give a note about the location of the declaration of DECL. */
2990
2991 static void
2992 inform_declaration (tree decl)
2993 {
2994 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2995 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2996 }
2997
2998 /* Build a function call to function FUNCTION with parameters PARAMS.
2999 ORIGTYPES, if not NULL, is a vector of types; each element is
3000 either NULL or the original type of the corresponding element in
3001 PARAMS. The original type may differ from TREE_TYPE of the
3002 parameter for enums. FUNCTION's data type may be a function type
3003 or pointer-to-function. This function changes the elements of
3004 PARAMS. */
3005
3006 tree
3007 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3008 tree function, vec<tree, va_gc> *params,
3009 vec<tree, va_gc> *origtypes)
3010 {
3011 tree fntype, fundecl = NULL_TREE;
3012 tree name = NULL_TREE, result;
3013 tree tem;
3014 int nargs;
3015 tree *argarray;
3016
3017
3018 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3019 STRIP_TYPE_NOPS (function);
3020
3021 /* Convert anything with function type to a pointer-to-function. */
3022 if (TREE_CODE (function) == FUNCTION_DECL)
3023 {
3024 name = DECL_NAME (function);
3025
3026 if (flag_tm)
3027 tm_malloc_replacement (function);
3028 fundecl = function;
3029 /* Atomic functions have type checking/casting already done. They are
3030 often rewritten and don't match the original parameter list. */
3031 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3032 origtypes = NULL;
3033
3034 if (flag_cilkplus
3035 && is_cilkplus_reduce_builtin (function))
3036 origtypes = NULL;
3037 }
3038 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3039 function = function_to_pointer_conversion (loc, function);
3040
3041 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3042 expressions, like those used for ObjC messenger dispatches. */
3043 if (params && !params->is_empty ())
3044 function = objc_rewrite_function_call (function, (*params)[0]);
3045
3046 function = c_fully_fold (function, false, NULL);
3047
3048 fntype = TREE_TYPE (function);
3049
3050 if (TREE_CODE (fntype) == ERROR_MARK)
3051 return error_mark_node;
3052
3053 if (!(TREE_CODE (fntype) == POINTER_TYPE
3054 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3055 {
3056 if (!flag_diagnostics_show_caret)
3057 error_at (loc,
3058 "called object %qE is not a function or function pointer",
3059 function);
3060 else if (DECL_P (function))
3061 {
3062 error_at (loc,
3063 "called object %qD is not a function or function pointer",
3064 function);
3065 inform_declaration (function);
3066 }
3067 else
3068 error_at (loc,
3069 "called object is not a function or function pointer");
3070 return error_mark_node;
3071 }
3072
3073 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3074 current_function_returns_abnormally = 1;
3075
3076 /* fntype now gets the type of function pointed to. */
3077 fntype = TREE_TYPE (fntype);
3078
3079 /* Convert the parameters to the types declared in the
3080 function prototype, or apply default promotions. */
3081
3082 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3083 origtypes, function, fundecl);
3084 if (nargs < 0)
3085 return error_mark_node;
3086
3087 /* Check that the function is called through a compatible prototype.
3088 If it is not, warn. */
3089 if (CONVERT_EXPR_P (function)
3090 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3091 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3092 && !comptypes (fntype, TREE_TYPE (tem)))
3093 {
3094 tree return_type = TREE_TYPE (fntype);
3095
3096 /* This situation leads to run-time undefined behavior. We can't,
3097 therefore, simply error unless we can prove that all possible
3098 executions of the program must execute the code. */
3099 warning_at (loc, 0, "function called through a non-compatible type");
3100
3101 if (VOID_TYPE_P (return_type)
3102 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3103 pedwarn (loc, 0,
3104 "function with qualified void return type called");
3105 }
3106
3107 argarray = vec_safe_address (params);
3108
3109 /* Check that arguments to builtin functions match the expectations. */
3110 if (fundecl
3111 && DECL_BUILT_IN (fundecl)
3112 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3113 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3114 argarray))
3115 return error_mark_node;
3116
3117 /* Check that the arguments to the function are valid. */
3118 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3119 nargs, argarray);
3120
3121 if (name != NULL_TREE
3122 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3123 {
3124 if (require_constant_value)
3125 result
3126 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3127 function, nargs, argarray);
3128 else
3129 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3130 function, nargs, argarray);
3131 if (TREE_CODE (result) == NOP_EXPR
3132 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3133 STRIP_TYPE_NOPS (result);
3134 }
3135 else
3136 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3137 function, nargs, argarray);
3138 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3139 later. */
3140 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3141 TREE_NO_WARNING (result) = 1;
3142
3143 /* In this improbable scenario, a nested function returns a VM type.
3144 Create a TARGET_EXPR so that the call always has a LHS, much as
3145 what the C++ FE does for functions returning non-PODs. */
3146 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3147 {
3148 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3149 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3150 NULL_TREE, NULL_TREE);
3151 }
3152
3153 if (VOID_TYPE_P (TREE_TYPE (result)))
3154 {
3155 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3156 pedwarn (loc, 0,
3157 "function with qualified void return type called");
3158 return result;
3159 }
3160 return require_complete_type (loc, result);
3161 }
3162
3163 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3164
3165 tree
3166 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3167 tree function, vec<tree, va_gc> *params,
3168 vec<tree, va_gc> *origtypes)
3169 {
3170 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3171 STRIP_TYPE_NOPS (function);
3172
3173 /* Convert anything with function type to a pointer-to-function. */
3174 if (TREE_CODE (function) == FUNCTION_DECL)
3175 {
3176 /* Implement type-directed function overloading for builtins.
3177 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3178 handle all the type checking. The result is a complete expression
3179 that implements this function call. */
3180 tree tem = resolve_overloaded_builtin (loc, function, params);
3181 if (tem)
3182 return tem;
3183 }
3184 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3185 }
3186 \f
3187 /* Convert the argument expressions in the vector VALUES
3188 to the types in the list TYPELIST.
3189
3190 If TYPELIST is exhausted, or when an element has NULL as its type,
3191 perform the default conversions.
3192
3193 ORIGTYPES is the original types of the expressions in VALUES. This
3194 holds the type of enum values which have been converted to integral
3195 types. It may be NULL.
3196
3197 FUNCTION is a tree for the called function. It is used only for
3198 error messages, where it is formatted with %qE.
3199
3200 This is also where warnings about wrong number of args are generated.
3201
3202 ARG_LOC are locations of function arguments (if any).
3203
3204 Returns the actual number of arguments processed (which may be less
3205 than the length of VALUES in some error situations), or -1 on
3206 failure. */
3207
3208 static int
3209 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3210 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3211 tree function, tree fundecl)
3212 {
3213 tree typetail, val;
3214 unsigned int parmnum;
3215 bool error_args = false;
3216 const bool type_generic = fundecl
3217 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3218 bool type_generic_remove_excess_precision = false;
3219 bool type_generic_overflow_p = false;
3220 tree selector;
3221
3222 /* Change pointer to function to the function itself for
3223 diagnostics. */
3224 if (TREE_CODE (function) == ADDR_EXPR
3225 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3226 function = TREE_OPERAND (function, 0);
3227
3228 /* Handle an ObjC selector specially for diagnostics. */
3229 selector = objc_message_selector ();
3230
3231 /* For type-generic built-in functions, determine whether excess
3232 precision should be removed (classification) or not
3233 (comparison). */
3234 if (type_generic
3235 && DECL_BUILT_IN (fundecl)
3236 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3237 {
3238 switch (DECL_FUNCTION_CODE (fundecl))
3239 {
3240 case BUILT_IN_ISFINITE:
3241 case BUILT_IN_ISINF:
3242 case BUILT_IN_ISINF_SIGN:
3243 case BUILT_IN_ISNAN:
3244 case BUILT_IN_ISNORMAL:
3245 case BUILT_IN_FPCLASSIFY:
3246 type_generic_remove_excess_precision = true;
3247 break;
3248
3249 case BUILT_IN_ADD_OVERFLOW_P:
3250 case BUILT_IN_SUB_OVERFLOW_P:
3251 case BUILT_IN_MUL_OVERFLOW_P:
3252 /* The last argument of these type-generic builtins
3253 should not be promoted. */
3254 type_generic_overflow_p = true;
3255 break;
3256
3257 default:
3258 break;
3259 }
3260 }
3261 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3262 return vec_safe_length (values);
3263
3264 /* Scan the given expressions and types, producing individual
3265 converted arguments. */
3266
3267 for (typetail = typelist, parmnum = 0;
3268 values && values->iterate (parmnum, &val);
3269 ++parmnum)
3270 {
3271 tree type = typetail ? TREE_VALUE (typetail) : 0;
3272 tree valtype = TREE_TYPE (val);
3273 tree rname = function;
3274 int argnum = parmnum + 1;
3275 const char *invalid_func_diag;
3276 bool excess_precision = false;
3277 bool npc;
3278 tree parmval;
3279 /* Some __atomic_* builtins have additional hidden argument at
3280 position 0. */
3281 location_t ploc
3282 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3283 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3284 : input_location;
3285
3286 if (type == void_type_node)
3287 {
3288 if (selector)
3289 error_at (loc, "too many arguments to method %qE", selector);
3290 else
3291 error_at (loc, "too many arguments to function %qE", function);
3292 inform_declaration (fundecl);
3293 return error_args ? -1 : (int) parmnum;
3294 }
3295
3296 if (selector && argnum > 2)
3297 {
3298 rname = selector;
3299 argnum -= 2;
3300 }
3301
3302 npc = null_pointer_constant_p (val);
3303
3304 /* If there is excess precision and a prototype, convert once to
3305 the required type rather than converting via the semantic
3306 type. Likewise without a prototype a float value represented
3307 as long double should be converted once to double. But for
3308 type-generic classification functions excess precision must
3309 be removed here. */
3310 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3311 && (type || !type_generic || !type_generic_remove_excess_precision))
3312 {
3313 val = TREE_OPERAND (val, 0);
3314 excess_precision = true;
3315 }
3316 val = c_fully_fold (val, false, NULL);
3317 STRIP_TYPE_NOPS (val);
3318
3319 val = require_complete_type (ploc, val);
3320
3321 /* Some floating-point arguments must be promoted to double when
3322 no type is specified by a prototype. This applies to
3323 arguments of type float, and to architecture-specific types
3324 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3325 bool promote_float_arg = false;
3326 if (type == NULL_TREE
3327 && TREE_CODE (valtype) == REAL_TYPE
3328 && (TYPE_PRECISION (valtype)
3329 <= TYPE_PRECISION (double_type_node))
3330 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3331 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3332 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3333 {
3334 /* Promote this argument, unless it has a _FloatN or
3335 _FloatNx type. */
3336 promote_float_arg = true;
3337 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3338 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3339 {
3340 promote_float_arg = false;
3341 break;
3342 }
3343 }
3344
3345 if (type != NULL_TREE)
3346 {
3347 /* Formal parm type is specified by a function prototype. */
3348
3349 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3350 {
3351 error_at (ploc, "type of formal parameter %d is incomplete",
3352 parmnum + 1);
3353 parmval = val;
3354 }
3355 else
3356 {
3357 tree origtype;
3358
3359 /* Optionally warn about conversions that
3360 differ from the default conversions. */
3361 if (warn_traditional_conversion || warn_traditional)
3362 {
3363 unsigned int formal_prec = TYPE_PRECISION (type);
3364
3365 if (INTEGRAL_TYPE_P (type)
3366 && TREE_CODE (valtype) == REAL_TYPE)
3367 warning_at (ploc, OPT_Wtraditional_conversion,
3368 "passing argument %d of %qE as integer rather "
3369 "than floating due to prototype",
3370 argnum, rname);
3371 if (INTEGRAL_TYPE_P (type)
3372 && TREE_CODE (valtype) == COMPLEX_TYPE)
3373 warning_at (ploc, OPT_Wtraditional_conversion,
3374 "passing argument %d of %qE as integer rather "
3375 "than complex due to prototype",
3376 argnum, rname);
3377 else if (TREE_CODE (type) == COMPLEX_TYPE
3378 && TREE_CODE (valtype) == REAL_TYPE)
3379 warning_at (ploc, OPT_Wtraditional_conversion,
3380 "passing argument %d of %qE as complex rather "
3381 "than floating due to prototype",
3382 argnum, rname);
3383 else if (TREE_CODE (type) == REAL_TYPE
3384 && INTEGRAL_TYPE_P (valtype))
3385 warning_at (ploc, OPT_Wtraditional_conversion,
3386 "passing argument %d of %qE as floating rather "
3387 "than integer due to prototype",
3388 argnum, rname);
3389 else if (TREE_CODE (type) == COMPLEX_TYPE
3390 && INTEGRAL_TYPE_P (valtype))
3391 warning_at (ploc, OPT_Wtraditional_conversion,
3392 "passing argument %d of %qE as complex rather "
3393 "than integer due to prototype",
3394 argnum, rname);
3395 else if (TREE_CODE (type) == REAL_TYPE
3396 && TREE_CODE (valtype) == COMPLEX_TYPE)
3397 warning_at (ploc, OPT_Wtraditional_conversion,
3398 "passing argument %d of %qE as floating rather "
3399 "than complex due to prototype",
3400 argnum, rname);
3401 /* ??? At some point, messages should be written about
3402 conversions between complex types, but that's too messy
3403 to do now. */
3404 else if (TREE_CODE (type) == REAL_TYPE
3405 && TREE_CODE (valtype) == REAL_TYPE)
3406 {
3407 /* Warn if any argument is passed as `float',
3408 since without a prototype it would be `double'. */
3409 if (formal_prec == TYPE_PRECISION (float_type_node)
3410 && type != dfloat32_type_node)
3411 warning_at (ploc, 0,
3412 "passing argument %d of %qE as %<float%> "
3413 "rather than %<double%> due to prototype",
3414 argnum, rname);
3415
3416 /* Warn if mismatch between argument and prototype
3417 for decimal float types. Warn of conversions with
3418 binary float types and of precision narrowing due to
3419 prototype. */
3420 else if (type != valtype
3421 && (type == dfloat32_type_node
3422 || type == dfloat64_type_node
3423 || type == dfloat128_type_node
3424 || valtype == dfloat32_type_node
3425 || valtype == dfloat64_type_node
3426 || valtype == dfloat128_type_node)
3427 && (formal_prec
3428 <= TYPE_PRECISION (valtype)
3429 || (type == dfloat128_type_node
3430 && (valtype
3431 != dfloat64_type_node
3432 && (valtype
3433 != dfloat32_type_node)))
3434 || (type == dfloat64_type_node
3435 && (valtype
3436 != dfloat32_type_node))))
3437 warning_at (ploc, 0,
3438 "passing argument %d of %qE as %qT "
3439 "rather than %qT due to prototype",
3440 argnum, rname, type, valtype);
3441
3442 }
3443 /* Detect integer changing in width or signedness.
3444 These warnings are only activated with
3445 -Wtraditional-conversion, not with -Wtraditional. */
3446 else if (warn_traditional_conversion
3447 && INTEGRAL_TYPE_P (type)
3448 && INTEGRAL_TYPE_P (valtype))
3449 {
3450 tree would_have_been = default_conversion (val);
3451 tree type1 = TREE_TYPE (would_have_been);
3452
3453 if (val == error_mark_node)
3454 /* VAL could have been of incomplete type. */;
3455 else if (TREE_CODE (type) == ENUMERAL_TYPE
3456 && (TYPE_MAIN_VARIANT (type)
3457 == TYPE_MAIN_VARIANT (valtype)))
3458 /* No warning if function asks for enum
3459 and the actual arg is that enum type. */
3460 ;
3461 else if (formal_prec != TYPE_PRECISION (type1))
3462 warning_at (ploc, OPT_Wtraditional_conversion,
3463 "passing argument %d of %qE "
3464 "with different width due to prototype",
3465 argnum, rname);
3466 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3467 ;
3468 /* Don't complain if the formal parameter type
3469 is an enum, because we can't tell now whether
3470 the value was an enum--even the same enum. */
3471 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3472 ;
3473 else if (TREE_CODE (val) == INTEGER_CST
3474 && int_fits_type_p (val, type))
3475 /* Change in signedness doesn't matter
3476 if a constant value is unaffected. */
3477 ;
3478 /* If the value is extended from a narrower
3479 unsigned type, it doesn't matter whether we
3480 pass it as signed or unsigned; the value
3481 certainly is the same either way. */
3482 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3483 && TYPE_UNSIGNED (valtype))
3484 ;
3485 else if (TYPE_UNSIGNED (type))
3486 warning_at (ploc, OPT_Wtraditional_conversion,
3487 "passing argument %d of %qE "
3488 "as unsigned due to prototype",
3489 argnum, rname);
3490 else
3491 warning_at (ploc, OPT_Wtraditional_conversion,
3492 "passing argument %d of %qE "
3493 "as signed due to prototype",
3494 argnum, rname);
3495 }
3496 }
3497
3498 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3499 sake of better warnings from convert_and_check. */
3500 if (excess_precision)
3501 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3502 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3503 parmval = convert_for_assignment (loc, ploc, type,
3504 val, origtype, ic_argpass,
3505 npc, fundecl, function,
3506 parmnum + 1);
3507
3508 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3509 && INTEGRAL_TYPE_P (type)
3510 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3511 parmval = default_conversion (parmval);
3512 }
3513 }
3514 else if (promote_float_arg)
3515 {
3516 if (type_generic)
3517 parmval = val;
3518 else
3519 {
3520 /* Convert `float' to `double'. */
3521 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3522 warning_at (ploc, OPT_Wdouble_promotion,
3523 "implicit conversion from %qT to %qT when passing "
3524 "argument to function",
3525 valtype, double_type_node);
3526 parmval = convert (double_type_node, val);
3527 }
3528 }
3529 else if ((excess_precision && !type_generic)
3530 || (type_generic_overflow_p && parmnum == 2))
3531 /* A "double" argument with excess precision being passed
3532 without a prototype or in variable arguments.
3533 The last argument of __builtin_*_overflow_p should not be
3534 promoted. */
3535 parmval = convert (valtype, val);
3536 else if ((invalid_func_diag =
3537 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3538 {
3539 error (invalid_func_diag);
3540 return -1;
3541 }
3542 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3543 {
3544 return -1;
3545 }
3546 else
3547 /* Convert `short' and `char' to full-size `int'. */
3548 parmval = default_conversion (val);
3549
3550 (*values)[parmnum] = parmval;
3551 if (parmval == error_mark_node)
3552 error_args = true;
3553
3554 if (typetail)
3555 typetail = TREE_CHAIN (typetail);
3556 }
3557
3558 gcc_assert (parmnum == vec_safe_length (values));
3559
3560 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3561 {
3562 error_at (loc, "too few arguments to function %qE", function);
3563 inform_declaration (fundecl);
3564 return -1;
3565 }
3566
3567 return error_args ? -1 : (int) parmnum;
3568 }
3569 \f
3570 /* This is the entry point used by the parser to build unary operators
3571 in the input. CODE, a tree_code, specifies the unary operator, and
3572 ARG is the operand. For unary plus, the C parser currently uses
3573 CONVERT_EXPR for code.
3574
3575 LOC is the location to use for the tree generated.
3576 */
3577
3578 struct c_expr
3579 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3580 {
3581 struct c_expr result;
3582
3583 result.original_code = code;
3584 result.original_type = NULL;
3585
3586 if (reject_gcc_builtin (arg.value))
3587 {
3588 result.value = error_mark_node;
3589 }
3590 else
3591 {
3592 result.value = build_unary_op (loc, code, arg.value, false);
3593
3594 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3595 overflow_warning (loc, result.value, arg.value);
3596 }
3597
3598 /* We are typically called when parsing a prefix token at LOC acting on
3599 ARG. Reflect this by updating the source range of the result to
3600 start at LOC and end at the end of ARG. */
3601 set_c_expr_source_range (&result,
3602 loc, arg.get_finish ());
3603
3604 return result;
3605 }
3606
3607 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3608
3609 static bool
3610 char_type_p (tree type)
3611 {
3612 return (type == char_type_node
3613 || type == unsigned_char_type_node
3614 || type == signed_char_type_node
3615 || type == char16_type_node
3616 || type == char32_type_node);
3617 }
3618
3619 /* This is the entry point used by the parser to build binary operators
3620 in the input. CODE, a tree_code, specifies the binary operator, and
3621 ARG1 and ARG2 are the operands. In addition to constructing the
3622 expression, we check for operands that were written with other binary
3623 operators in a way that is likely to confuse the user.
3624
3625 LOCATION is the location of the binary operator. */
3626
3627 struct c_expr
3628 parser_build_binary_op (location_t location, enum tree_code code,
3629 struct c_expr arg1, struct c_expr arg2)
3630 {
3631 struct c_expr result;
3632
3633 enum tree_code code1 = arg1.original_code;
3634 enum tree_code code2 = arg2.original_code;
3635 tree type1 = (arg1.original_type
3636 ? arg1.original_type
3637 : TREE_TYPE (arg1.value));
3638 tree type2 = (arg2.original_type
3639 ? arg2.original_type
3640 : TREE_TYPE (arg2.value));
3641
3642 result.value = build_binary_op (location, code,
3643 arg1.value, arg2.value, 1);
3644 result.original_code = code;
3645 result.original_type = NULL;
3646
3647 if (TREE_CODE (result.value) == ERROR_MARK)
3648 {
3649 set_c_expr_source_range (&result,
3650 arg1.get_start (),
3651 arg2.get_finish ());
3652 return result;
3653 }
3654
3655 if (location != UNKNOWN_LOCATION)
3656 protected_set_expr_location (result.value, location);
3657
3658 set_c_expr_source_range (&result,
3659 arg1.get_start (),
3660 arg2.get_finish ());
3661
3662 /* Check for cases such as x+y<<z which users are likely
3663 to misinterpret. */
3664 if (warn_parentheses)
3665 warn_about_parentheses (location, code, code1, arg1.value, code2,
3666 arg2.value);
3667
3668 if (warn_logical_op)
3669 warn_logical_operator (location, code, TREE_TYPE (result.value),
3670 code1, arg1.value, code2, arg2.value);
3671
3672 if (warn_tautological_compare)
3673 {
3674 tree lhs = arg1.value;
3675 tree rhs = arg2.value;
3676 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3677 {
3678 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3679 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3680 lhs = NULL_TREE;
3681 else
3682 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3683 }
3684 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3685 {
3686 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3687 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3688 rhs = NULL_TREE;
3689 else
3690 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3691 }
3692 if (lhs != NULL_TREE && rhs != NULL_TREE)
3693 warn_tautological_cmp (location, code, lhs, rhs);
3694 }
3695
3696 if (warn_logical_not_paren
3697 && TREE_CODE_CLASS (code) == tcc_comparison
3698 && code1 == TRUTH_NOT_EXPR
3699 && code2 != TRUTH_NOT_EXPR
3700 /* Avoid warning for !!x == y. */
3701 && (TREE_CODE (arg1.value) != NE_EXPR
3702 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3703 {
3704 /* Avoid warning for !b == y where b has _Bool type. */
3705 tree t = integer_zero_node;
3706 if (TREE_CODE (arg1.value) == EQ_EXPR
3707 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3708 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3709 {
3710 t = TREE_OPERAND (arg1.value, 0);
3711 do
3712 {
3713 if (TREE_TYPE (t) != integer_type_node)
3714 break;
3715 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3716 t = C_MAYBE_CONST_EXPR_EXPR (t);
3717 else if (CONVERT_EXPR_P (t))
3718 t = TREE_OPERAND (t, 0);
3719 else
3720 break;
3721 }
3722 while (1);
3723 }
3724 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3725 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3726 }
3727
3728 /* Warn about comparisons against string literals, with the exception
3729 of testing for equality or inequality of a string literal with NULL. */
3730 if (code == EQ_EXPR || code == NE_EXPR)
3731 {
3732 if ((code1 == STRING_CST
3733 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3734 || (code2 == STRING_CST
3735 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3736 warning_at (location, OPT_Waddress,
3737 "comparison with string literal results in unspecified behavior");
3738 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3739 if (POINTER_TYPE_P (type1)
3740 && null_pointer_constant_p (arg2.value)
3741 && char_type_p (type2)
3742 && warning_at (location, OPT_Wpointer_compare,
3743 "comparison between pointer and zero character "
3744 "constant"))
3745 inform (arg1.get_start (), "did you mean to dereference the pointer?");
3746 else if (POINTER_TYPE_P (type2)
3747 && null_pointer_constant_p (arg1.value)
3748 && char_type_p (type1)
3749 && warning_at (location, OPT_Wpointer_compare,
3750 "comparison between pointer and zero character "
3751 "constant"))
3752 inform (arg2.get_start (), "did you mean to dereference the pointer?");
3753 }
3754 else if (TREE_CODE_CLASS (code) == tcc_comparison
3755 && (code1 == STRING_CST || code2 == STRING_CST))
3756 warning_at (location, OPT_Waddress,
3757 "comparison with string literal results in unspecified behavior");
3758
3759 if (TREE_OVERFLOW_P (result.value)
3760 && !TREE_OVERFLOW_P (arg1.value)
3761 && !TREE_OVERFLOW_P (arg2.value))
3762 overflow_warning (location, result.value);
3763
3764 /* Warn about comparisons of different enum types. */
3765 if (warn_enum_compare
3766 && TREE_CODE_CLASS (code) == tcc_comparison
3767 && TREE_CODE (type1) == ENUMERAL_TYPE
3768 && TREE_CODE (type2) == ENUMERAL_TYPE
3769 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3770 warning_at (location, OPT_Wenum_compare,
3771 "comparison between %qT and %qT",
3772 type1, type2);
3773
3774 return result;
3775 }
3776 \f
3777 /* Return a tree for the difference of pointers OP0 and OP1.
3778 The resulting tree has type int. */
3779
3780 static tree
3781 pointer_diff (location_t loc, tree op0, tree op1)
3782 {
3783 tree restype = ptrdiff_type_node;
3784 tree result, inttype;
3785
3786 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3787 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3788 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3789 tree orig_op1 = op1;
3790
3791 /* If the operands point into different address spaces, we need to
3792 explicitly convert them to pointers into the common address space
3793 before we can subtract the numerical address values. */
3794 if (as0 != as1)
3795 {
3796 addr_space_t as_common;
3797 tree common_type;
3798
3799 /* Determine the common superset address space. This is guaranteed
3800 to exist because the caller verified that comp_target_types
3801 returned non-zero. */
3802 if (!addr_space_superset (as0, as1, &as_common))
3803 gcc_unreachable ();
3804
3805 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3806 op0 = convert (common_type, op0);
3807 op1 = convert (common_type, op1);
3808 }
3809
3810 /* Determine integer type to perform computations in. This will usually
3811 be the same as the result type (ptrdiff_t), but may need to be a wider
3812 type if pointers for the address space are wider than ptrdiff_t. */
3813 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3814 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3815 else
3816 inttype = restype;
3817
3818 if (TREE_CODE (target_type) == VOID_TYPE)
3819 pedwarn (loc, OPT_Wpointer_arith,
3820 "pointer of type %<void *%> used in subtraction");
3821 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3822 pedwarn (loc, OPT_Wpointer_arith,
3823 "pointer to a function used in subtraction");
3824
3825 /* First do the subtraction as integers;
3826 then drop through to build the divide operator.
3827 Do not do default conversions on the minus operator
3828 in case restype is a short type. */
3829
3830 op0 = build_binary_op (loc,
3831 MINUS_EXPR, convert (inttype, op0),
3832 convert (inttype, op1), 0);
3833 /* This generates an error if op1 is pointer to incomplete type. */
3834 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3835 error_at (loc, "arithmetic on pointer to an incomplete type");
3836
3837 op1 = c_size_in_bytes (target_type);
3838
3839 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3840 error_at (loc, "arithmetic on pointer to an empty aggregate");
3841
3842 /* Divide by the size, in easiest possible way. */
3843 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3844 op0, convert (inttype, op1));
3845
3846 /* Convert to final result type if necessary. */
3847 return convert (restype, result);
3848 }
3849 \f
3850 /* Expand atomic compound assignments into an appropriate sequence as
3851 specified by the C11 standard section 6.5.16.2.
3852
3853 _Atomic T1 E1
3854 T2 E2
3855 E1 op= E2
3856
3857 This sequence is used for all types for which these operations are
3858 supported.
3859
3860 In addition, built-in versions of the 'fe' prefixed routines may
3861 need to be invoked for floating point (real, complex or vector) when
3862 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3863
3864 T1 newval;
3865 T1 old;
3866 T1 *addr
3867 T2 val
3868 fenv_t fenv
3869
3870 addr = &E1;
3871 val = (E2);
3872 __atomic_load (addr, &old, SEQ_CST);
3873 feholdexcept (&fenv);
3874 loop:
3875 newval = old op val;
3876 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3877 SEQ_CST))
3878 goto done;
3879 feclearexcept (FE_ALL_EXCEPT);
3880 goto loop:
3881 done:
3882 feupdateenv (&fenv);
3883
3884 The compiler will issue the __atomic_fetch_* built-in when possible,
3885 otherwise it will generate the generic form of the atomic operations.
3886 This requires temp(s) and has their address taken. The atomic processing
3887 is smart enough to figure out when the size of an object can utilize
3888 a lock-free version, and convert the built-in call to the appropriate
3889 lock-free routine. The optimizers will then dispose of any temps that
3890 are no longer required, and lock-free implementations are utilized as
3891 long as there is target support for the required size.
3892
3893 If the operator is NOP_EXPR, then this is a simple assignment, and
3894 an __atomic_store is issued to perform the assignment rather than
3895 the above loop. */
3896
3897 /* Build an atomic assignment at LOC, expanding into the proper
3898 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3899 the result of the operation, unless RETURN_OLD_P, in which case
3900 return the old value of LHS (this is only for postincrement and
3901 postdecrement). */
3902
3903 static tree
3904 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3905 tree rhs, bool return_old_p)
3906 {
3907 tree fndecl, func_call;
3908 vec<tree, va_gc> *params;
3909 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3910 tree old, old_addr;
3911 tree compound_stmt;
3912 tree stmt, goto_stmt;
3913 tree loop_label, loop_decl, done_label, done_decl;
3914
3915 tree lhs_type = TREE_TYPE (lhs);
3916 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
3917 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3918 tree rhs_type = TREE_TYPE (rhs);
3919
3920 gcc_assert (TYPE_ATOMIC (lhs_type));
3921
3922 if (return_old_p)
3923 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3924
3925 /* Allocate enough vector items for a compare_exchange. */
3926 vec_alloc (params, 6);
3927
3928 /* Create a compound statement to hold the sequence of statements
3929 with a loop. */
3930 compound_stmt = c_begin_compound_stmt (false);
3931
3932 /* Fold the RHS if it hasn't already been folded. */
3933 if (modifycode != NOP_EXPR)
3934 rhs = c_fully_fold (rhs, false, NULL);
3935
3936 /* Remove the qualifiers for the rest of the expressions and create
3937 the VAL temp variable to hold the RHS. */
3938 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3939 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3940 val = create_tmp_var_raw (nonatomic_rhs_type);
3941 TREE_ADDRESSABLE (val) = 1;
3942 TREE_NO_WARNING (val) = 1;
3943 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3944 NULL_TREE);
3945 SET_EXPR_LOCATION (rhs, loc);
3946 add_stmt (rhs);
3947
3948 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3949 an atomic_store. */
3950 if (modifycode == NOP_EXPR)
3951 {
3952 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3953 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
3954 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3955 params->quick_push (lhs_addr);
3956 params->quick_push (rhs);
3957 params->quick_push (seq_cst);
3958 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3959 add_stmt (func_call);
3960
3961 /* Finish the compound statement. */
3962 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3963
3964 /* VAL is the value which was stored, return a COMPOUND_STMT of
3965 the statement and that value. */
3966 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3967 }
3968
3969 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3970 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3971 isn't applicable for such builtins. ??? Do we want to handle enums? */
3972 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3973 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3974 {
3975 built_in_function fncode;
3976 switch (modifycode)
3977 {
3978 case PLUS_EXPR:
3979 case POINTER_PLUS_EXPR:
3980 fncode = (return_old_p
3981 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3982 : BUILT_IN_ATOMIC_ADD_FETCH_N);
3983 break;
3984 case MINUS_EXPR:
3985 fncode = (return_old_p
3986 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3987 : BUILT_IN_ATOMIC_SUB_FETCH_N);
3988 break;
3989 case BIT_AND_EXPR:
3990 fncode = (return_old_p
3991 ? BUILT_IN_ATOMIC_FETCH_AND_N
3992 : BUILT_IN_ATOMIC_AND_FETCH_N);
3993 break;
3994 case BIT_IOR_EXPR:
3995 fncode = (return_old_p
3996 ? BUILT_IN_ATOMIC_FETCH_OR_N
3997 : BUILT_IN_ATOMIC_OR_FETCH_N);
3998 break;
3999 case BIT_XOR_EXPR:
4000 fncode = (return_old_p
4001 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4002 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4003 break;
4004 default:
4005 goto cas_loop;
4006 }
4007
4008 /* We can only use "_1" through "_16" variants of the atomic fetch
4009 built-ins. */
4010 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4011 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4012 goto cas_loop;
4013
4014 /* If this is a pointer type, we need to multiply by the size of
4015 the pointer target type. */
4016 if (POINTER_TYPE_P (lhs_type))
4017 {
4018 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4019 /* ??? This would introduce -Wdiscarded-qualifiers
4020 warning: __atomic_fetch_* expect volatile void *
4021 type as the first argument. (Assignments between
4022 atomic and non-atomic objects are OK.) */
4023 || TYPE_RESTRICT (lhs_type))
4024 goto cas_loop;
4025 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4026 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4027 convert (ptrdiff_type_node, rhs),
4028 convert (ptrdiff_type_node, sz));
4029 }
4030
4031 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4032 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4033 fndecl = builtin_decl_explicit (fncode);
4034 params->quick_push (lhs_addr);
4035 params->quick_push (rhs);
4036 params->quick_push (seq_cst);
4037 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4038
4039 newval = create_tmp_var_raw (nonatomic_lhs_type);
4040 TREE_ADDRESSABLE (newval) = 1;
4041 TREE_NO_WARNING (newval) = 1;
4042 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4043 NULL_TREE, NULL_TREE);
4044 SET_EXPR_LOCATION (rhs, loc);
4045 add_stmt (rhs);
4046
4047 /* Finish the compound statement. */
4048 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4049
4050 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4051 the statement and that value. */
4052 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4053 }
4054
4055 cas_loop:
4056 /* Create the variables and labels required for the op= form. */
4057 old = create_tmp_var_raw (nonatomic_lhs_type);
4058 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4059 TREE_ADDRESSABLE (old) = 1;
4060 TREE_NO_WARNING (old) = 1;
4061
4062 newval = create_tmp_var_raw (nonatomic_lhs_type);
4063 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4064 TREE_ADDRESSABLE (newval) = 1;
4065 TREE_NO_WARNING (newval) = 1;
4066
4067 loop_decl = create_artificial_label (loc);
4068 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4069
4070 done_decl = create_artificial_label (loc);
4071 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4072
4073 /* __atomic_load (addr, &old, SEQ_CST). */
4074 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4075 params->quick_push (lhs_addr);
4076 params->quick_push (old_addr);
4077 params->quick_push (seq_cst);
4078 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4079 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4080 NULL_TREE);
4081 add_stmt (old);
4082 params->truncate (0);
4083
4084 /* Create the expressions for floating-point environment
4085 manipulation, if required. */
4086 bool need_fenv = (flag_trapping_math
4087 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4088 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4089 if (need_fenv)
4090 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4091
4092 if (hold_call)
4093 add_stmt (hold_call);
4094
4095 /* loop: */
4096 add_stmt (loop_label);
4097
4098 /* newval = old + val; */
4099 rhs = build_binary_op (loc, modifycode, old, val, 1);
4100 rhs = c_fully_fold (rhs, false, NULL);
4101 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4102 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4103 NULL_TREE, 0);
4104 if (rhs != error_mark_node)
4105 {
4106 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4107 NULL_TREE);
4108 SET_EXPR_LOCATION (rhs, loc);
4109 add_stmt (rhs);
4110 }
4111
4112 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4113 goto done; */
4114 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4115 params->quick_push (lhs_addr);
4116 params->quick_push (old_addr);
4117 params->quick_push (newval_addr);
4118 params->quick_push (integer_zero_node);
4119 params->quick_push (seq_cst);
4120 params->quick_push (seq_cst);
4121 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4122
4123 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4124 SET_EXPR_LOCATION (goto_stmt, loc);
4125
4126 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4127 SET_EXPR_LOCATION (stmt, loc);
4128 add_stmt (stmt);
4129
4130 if (clear_call)
4131 add_stmt (clear_call);
4132
4133 /* goto loop; */
4134 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4135 SET_EXPR_LOCATION (goto_stmt, loc);
4136 add_stmt (goto_stmt);
4137
4138 /* done: */
4139 add_stmt (done_label);
4140
4141 if (update_call)
4142 add_stmt (update_call);
4143
4144 /* Finish the compound statement. */
4145 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4146
4147 /* NEWVAL is the value that was successfully stored, return a
4148 COMPOUND_EXPR of the statement and the appropriate value. */
4149 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4150 return_old_p ? old : newval);
4151 }
4152
4153 /* Construct and perhaps optimize a tree representation
4154 for a unary operation. CODE, a tree_code, specifies the operation
4155 and XARG is the operand.
4156 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4157 promotions (such as from short to int).
4158 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4159 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4160 to pointers in C99.
4161
4162 LOCATION is the location of the operator. */
4163
4164 tree
4165 build_unary_op (location_t location, enum tree_code code, tree xarg,
4166 bool noconvert)
4167 {
4168 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4169 tree arg = xarg;
4170 tree argtype = NULL_TREE;
4171 enum tree_code typecode;
4172 tree val;
4173 tree ret = error_mark_node;
4174 tree eptype = NULL_TREE;
4175 const char *invalid_op_diag;
4176 bool int_operands;
4177
4178 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4179 if (int_operands)
4180 arg = remove_c_maybe_const_expr (arg);
4181
4182 if (code != ADDR_EXPR)
4183 arg = require_complete_type (location, arg);
4184
4185 typecode = TREE_CODE (TREE_TYPE (arg));
4186 if (typecode == ERROR_MARK)
4187 return error_mark_node;
4188 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4189 typecode = INTEGER_TYPE;
4190
4191 if ((invalid_op_diag
4192 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4193 {
4194 error_at (location, invalid_op_diag);
4195 return error_mark_node;
4196 }
4197
4198 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4199 {
4200 eptype = TREE_TYPE (arg);
4201 arg = TREE_OPERAND (arg, 0);
4202 }
4203
4204 switch (code)
4205 {
4206 case CONVERT_EXPR:
4207 /* This is used for unary plus, because a CONVERT_EXPR
4208 is enough to prevent anybody from looking inside for
4209 associativity, but won't generate any code. */
4210 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4211 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4212 || typecode == VECTOR_TYPE))
4213 {
4214 error_at (location, "wrong type argument to unary plus");
4215 return error_mark_node;
4216 }
4217 else if (!noconvert)
4218 arg = default_conversion (arg);
4219 arg = non_lvalue_loc (location, arg);
4220 break;
4221
4222 case NEGATE_EXPR:
4223 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4224 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4225 || typecode == VECTOR_TYPE))
4226 {
4227 error_at (location, "wrong type argument to unary minus");
4228 return error_mark_node;
4229 }
4230 else if (!noconvert)
4231 arg = default_conversion (arg);
4232 break;
4233
4234 case BIT_NOT_EXPR:
4235 /* ~ works on integer types and non float vectors. */
4236 if (typecode == INTEGER_TYPE
4237 || (typecode == VECTOR_TYPE
4238 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4239 {
4240 tree e = arg;
4241
4242 /* Warn if the expression has boolean value. */
4243 while (TREE_CODE (e) == COMPOUND_EXPR)
4244 e = TREE_OPERAND (e, 1);
4245
4246 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4247 || truth_value_p (TREE_CODE (e)))
4248 && warning_at (location, OPT_Wbool_operation,
4249 "%<~%> on a boolean expression"))
4250 {
4251 gcc_rich_location richloc (location);
4252 richloc.add_fixit_insert_before (location, "!");
4253 inform_at_rich_loc (&richloc, "did you mean to use logical "
4254 "not?");
4255 }
4256 if (!noconvert)
4257 arg = default_conversion (arg);
4258 }
4259 else if (typecode == COMPLEX_TYPE)
4260 {
4261 code = CONJ_EXPR;
4262 pedwarn (location, OPT_Wpedantic,
4263 "ISO C does not support %<~%> for complex conjugation");
4264 if (!noconvert)
4265 arg = default_conversion (arg);
4266 }
4267 else
4268 {
4269 error_at (location, "wrong type argument to bit-complement");
4270 return error_mark_node;
4271 }
4272 break;
4273
4274 case ABS_EXPR:
4275 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4276 {
4277 error_at (location, "wrong type argument to abs");
4278 return error_mark_node;
4279 }
4280 else if (!noconvert)
4281 arg = default_conversion (arg);
4282 break;
4283
4284 case CONJ_EXPR:
4285 /* Conjugating a real value is a no-op, but allow it anyway. */
4286 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4287 || typecode == COMPLEX_TYPE))
4288 {
4289 error_at (location, "wrong type argument to conjugation");
4290 return error_mark_node;
4291 }
4292 else if (!noconvert)
4293 arg = default_conversion (arg);
4294 break;
4295
4296 case TRUTH_NOT_EXPR:
4297 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4298 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4299 && typecode != COMPLEX_TYPE)
4300 {
4301 error_at (location,
4302 "wrong type argument to unary exclamation mark");
4303 return error_mark_node;
4304 }
4305 if (int_operands)
4306 {
4307 arg = c_objc_common_truthvalue_conversion (location, xarg);
4308 arg = remove_c_maybe_const_expr (arg);
4309 }
4310 else
4311 arg = c_objc_common_truthvalue_conversion (location, arg);
4312 ret = invert_truthvalue_loc (location, arg);
4313 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4314 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4315 location = EXPR_LOCATION (ret);
4316 goto return_build_unary_op;
4317
4318 case REALPART_EXPR:
4319 case IMAGPART_EXPR:
4320 ret = build_real_imag_expr (location, code, arg);
4321 if (ret == error_mark_node)
4322 return error_mark_node;
4323 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4324 eptype = TREE_TYPE (eptype);
4325 goto return_build_unary_op;
4326
4327 case PREINCREMENT_EXPR:
4328 case POSTINCREMENT_EXPR:
4329 case PREDECREMENT_EXPR:
4330 case POSTDECREMENT_EXPR:
4331
4332 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4333 {
4334 tree inner = build_unary_op (location, code,
4335 C_MAYBE_CONST_EXPR_EXPR (arg),
4336 noconvert);
4337 if (inner == error_mark_node)
4338 return error_mark_node;
4339 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4340 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4341 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4342 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4343 goto return_build_unary_op;
4344 }
4345
4346 /* Complain about anything that is not a true lvalue. In
4347 Objective-C, skip this check for property_refs. */
4348 if (!objc_is_property_ref (arg)
4349 && !lvalue_or_else (location,
4350 arg, ((code == PREINCREMENT_EXPR
4351 || code == POSTINCREMENT_EXPR)
4352 ? lv_increment
4353 : lv_decrement)))
4354 return error_mark_node;
4355
4356 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4357 {
4358 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4359 warning_at (location, OPT_Wc___compat,
4360 "increment of enumeration value is invalid in C++");
4361 else
4362 warning_at (location, OPT_Wc___compat,
4363 "decrement of enumeration value is invalid in C++");
4364 }
4365
4366 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4367 {
4368 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4369 warning_at (location, OPT_Wbool_operation,
4370 "increment of a boolean expression");
4371 else
4372 warning_at (location, OPT_Wbool_operation,
4373 "decrement of a boolean expression");
4374 }
4375
4376 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4377 arg = c_fully_fold (arg, false, NULL);
4378
4379 bool atomic_op;
4380 atomic_op = really_atomic_lvalue (arg);
4381
4382 /* Increment or decrement the real part of the value,
4383 and don't change the imaginary part. */
4384 if (typecode == COMPLEX_TYPE)
4385 {
4386 tree real, imag;
4387
4388 pedwarn (location, OPT_Wpedantic,
4389 "ISO C does not support %<++%> and %<--%> on complex types");
4390
4391 if (!atomic_op)
4392 {
4393 arg = stabilize_reference (arg);
4394 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4395 true);
4396 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4397 true);
4398 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4399 if (real == error_mark_node || imag == error_mark_node)
4400 return error_mark_node;
4401 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4402 real, imag);
4403 goto return_build_unary_op;
4404 }
4405 }
4406
4407 /* Report invalid types. */
4408
4409 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4410 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4411 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4412 {
4413 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4414 error_at (location, "wrong type argument to increment");
4415 else
4416 error_at (location, "wrong type argument to decrement");
4417
4418 return error_mark_node;
4419 }
4420
4421 {
4422 tree inc;
4423
4424 argtype = TREE_TYPE (arg);
4425
4426 /* Compute the increment. */
4427
4428 if (typecode == POINTER_TYPE)
4429 {
4430 /* If pointer target is an incomplete type,
4431 we just cannot know how to do the arithmetic. */
4432 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4433 {
4434 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4435 error_at (location,
4436 "increment of pointer to an incomplete type %qT",
4437 TREE_TYPE (argtype));
4438 else
4439 error_at (location,
4440 "decrement of pointer to an incomplete type %qT",
4441 TREE_TYPE (argtype));
4442 }
4443 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4444 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4445 {
4446 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4447 pedwarn (location, OPT_Wpointer_arith,
4448 "wrong type argument to increment");
4449 else
4450 pedwarn (location, OPT_Wpointer_arith,
4451 "wrong type argument to decrement");
4452 }
4453
4454 inc = c_size_in_bytes (TREE_TYPE (argtype));
4455 inc = convert_to_ptrofftype_loc (location, inc);
4456 }
4457 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4458 {
4459 /* For signed fract types, we invert ++ to -- or
4460 -- to ++, and change inc from 1 to -1, because
4461 it is not possible to represent 1 in signed fract constants.
4462 For unsigned fract types, the result always overflows and
4463 we get an undefined (original) or the maximum value. */
4464 if (code == PREINCREMENT_EXPR)
4465 code = PREDECREMENT_EXPR;
4466 else if (code == PREDECREMENT_EXPR)
4467 code = PREINCREMENT_EXPR;
4468 else if (code == POSTINCREMENT_EXPR)
4469 code = POSTDECREMENT_EXPR;
4470 else /* code == POSTDECREMENT_EXPR */
4471 code = POSTINCREMENT_EXPR;
4472
4473 inc = integer_minus_one_node;
4474 inc = convert (argtype, inc);
4475 }
4476 else
4477 {
4478 inc = VECTOR_TYPE_P (argtype)
4479 ? build_one_cst (argtype)
4480 : integer_one_node;
4481 inc = convert (argtype, inc);
4482 }
4483
4484 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4485 need to ask Objective-C to build the increment or decrement
4486 expression for it. */
4487 if (objc_is_property_ref (arg))
4488 return objc_build_incr_expr_for_property_ref (location, code,
4489 arg, inc);
4490
4491 /* Report a read-only lvalue. */
4492 if (TYPE_READONLY (argtype))
4493 {
4494 readonly_error (location, arg,
4495 ((code == PREINCREMENT_EXPR
4496 || code == POSTINCREMENT_EXPR)
4497 ? lv_increment : lv_decrement));
4498 return error_mark_node;
4499 }
4500 else if (TREE_READONLY (arg))
4501 readonly_warning (arg,
4502 ((code == PREINCREMENT_EXPR
4503 || code == POSTINCREMENT_EXPR)
4504 ? lv_increment : lv_decrement));
4505
4506 /* If the argument is atomic, use the special code sequences for
4507 atomic compound assignment. */
4508 if (atomic_op)
4509 {
4510 arg = stabilize_reference (arg);
4511 ret = build_atomic_assign (location, arg,
4512 ((code == PREINCREMENT_EXPR
4513 || code == POSTINCREMENT_EXPR)
4514 ? PLUS_EXPR
4515 : MINUS_EXPR),
4516 (FRACT_MODE_P (TYPE_MODE (argtype))
4517 ? inc
4518 : integer_one_node),
4519 (code == POSTINCREMENT_EXPR
4520 || code == POSTDECREMENT_EXPR));
4521 goto return_build_unary_op;
4522 }
4523
4524 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4525 val = boolean_increment (code, arg);
4526 else
4527 val = build2 (code, TREE_TYPE (arg), arg, inc);
4528 TREE_SIDE_EFFECTS (val) = 1;
4529 if (TREE_CODE (val) != code)
4530 TREE_NO_WARNING (val) = 1;
4531 ret = val;
4532 goto return_build_unary_op;
4533 }
4534
4535 case ADDR_EXPR:
4536 /* Note that this operation never does default_conversion. */
4537
4538 /* The operand of unary '&' must be an lvalue (which excludes
4539 expressions of type void), or, in C99, the result of a [] or
4540 unary '*' operator. */
4541 if (VOID_TYPE_P (TREE_TYPE (arg))
4542 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4543 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4544 pedwarn (location, 0, "taking address of expression of type %<void%>");
4545
4546 /* Let &* cancel out to simplify resulting code. */
4547 if (INDIRECT_REF_P (arg))
4548 {
4549 /* Don't let this be an lvalue. */
4550 if (lvalue_p (TREE_OPERAND (arg, 0)))
4551 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4552 ret = TREE_OPERAND (arg, 0);
4553 goto return_build_unary_op;
4554 }
4555
4556 /* Anything not already handled and not a true memory reference
4557 or a non-lvalue array is an error. */
4558 if (typecode != FUNCTION_TYPE && !noconvert
4559 && !lvalue_or_else (location, arg, lv_addressof))
4560 return error_mark_node;
4561
4562 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4563 folding later. */
4564 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4565 {
4566 tree inner = build_unary_op (location, code,
4567 C_MAYBE_CONST_EXPR_EXPR (arg),
4568 noconvert);
4569 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4570 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4571 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4572 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4573 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4574 goto return_build_unary_op;
4575 }
4576
4577 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4578 argtype = TREE_TYPE (arg);
4579
4580 /* If the lvalue is const or volatile, merge that into the type
4581 to which the address will point. This is only needed
4582 for function types. */
4583 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4584 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4585 && TREE_CODE (argtype) == FUNCTION_TYPE)
4586 {
4587 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4588 int quals = orig_quals;
4589
4590 if (TREE_READONLY (arg))
4591 quals |= TYPE_QUAL_CONST;
4592 if (TREE_THIS_VOLATILE (arg))
4593 quals |= TYPE_QUAL_VOLATILE;
4594
4595 argtype = c_build_qualified_type (argtype, quals);
4596 }
4597
4598 switch (TREE_CODE (arg))
4599 {
4600 case COMPONENT_REF:
4601 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4602 {
4603 error_at (location, "cannot take address of bit-field %qD",
4604 TREE_OPERAND (arg, 1));
4605 return error_mark_node;
4606 }
4607
4608 /* fall through */
4609
4610 case ARRAY_REF:
4611 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4612 {
4613 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4614 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4615 {
4616 error_at (location, "cannot take address of scalar with "
4617 "reverse storage order");
4618 return error_mark_node;
4619 }
4620
4621 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4622 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4623 warning_at (location, OPT_Wscalar_storage_order,
4624 "address of array with reverse scalar storage "
4625 "order requested");
4626 }
4627
4628 default:
4629 break;
4630 }
4631
4632 if (!c_mark_addressable (arg))
4633 return error_mark_node;
4634
4635 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4636 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4637
4638 argtype = build_pointer_type (argtype);
4639
4640 /* ??? Cope with user tricks that amount to offsetof. Delete this
4641 when we have proper support for integer constant expressions. */
4642 val = get_base_address (arg);
4643 if (val && INDIRECT_REF_P (val)
4644 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4645 {
4646 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4647 goto return_build_unary_op;
4648 }
4649
4650 val = build1 (ADDR_EXPR, argtype, arg);
4651
4652 ret = val;
4653 goto return_build_unary_op;
4654
4655 default:
4656 gcc_unreachable ();
4657 }
4658
4659 if (argtype == NULL_TREE)
4660 argtype = TREE_TYPE (arg);
4661 if (TREE_CODE (arg) == INTEGER_CST)
4662 ret = (require_constant_value
4663 ? fold_build1_initializer_loc (location, code, argtype, arg)
4664 : fold_build1_loc (location, code, argtype, arg));
4665 else
4666 ret = build1 (code, argtype, arg);
4667 return_build_unary_op:
4668 gcc_assert (ret != error_mark_node);
4669 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4670 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4671 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4672 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4673 ret = note_integer_operands (ret);
4674 if (eptype)
4675 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4676 protected_set_expr_location (ret, location);
4677 return ret;
4678 }
4679
4680 /* Return nonzero if REF is an lvalue valid for this language.
4681 Lvalues can be assigned, unless their type has TYPE_READONLY.
4682 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4683
4684 bool
4685 lvalue_p (const_tree ref)
4686 {
4687 const enum tree_code code = TREE_CODE (ref);
4688
4689 switch (code)
4690 {
4691 case REALPART_EXPR:
4692 case IMAGPART_EXPR:
4693 case COMPONENT_REF:
4694 return lvalue_p (TREE_OPERAND (ref, 0));
4695
4696 case C_MAYBE_CONST_EXPR:
4697 return lvalue_p (TREE_OPERAND (ref, 1));
4698
4699 case COMPOUND_LITERAL_EXPR:
4700 case STRING_CST:
4701 return true;
4702
4703 case INDIRECT_REF:
4704 case ARRAY_REF:
4705 case ARRAY_NOTATION_REF:
4706 case VAR_DECL:
4707 case PARM_DECL:
4708 case RESULT_DECL:
4709 case ERROR_MARK:
4710 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4711 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4712
4713 case BIND_EXPR:
4714 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4715
4716 default:
4717 return false;
4718 }
4719 }
4720 \f
4721 /* Give a warning for storing in something that is read-only in GCC
4722 terms but not const in ISO C terms. */
4723
4724 static void
4725 readonly_warning (tree arg, enum lvalue_use use)
4726 {
4727 switch (use)
4728 {
4729 case lv_assign:
4730 warning (0, "assignment of read-only location %qE", arg);
4731 break;
4732 case lv_increment:
4733 warning (0, "increment of read-only location %qE", arg);
4734 break;
4735 case lv_decrement:
4736 warning (0, "decrement of read-only location %qE", arg);
4737 break;
4738 default:
4739 gcc_unreachable ();
4740 }
4741 return;
4742 }
4743
4744
4745 /* Return nonzero if REF is an lvalue valid for this language;
4746 otherwise, print an error message and return zero. USE says
4747 how the lvalue is being used and so selects the error message.
4748 LOCATION is the location at which any error should be reported. */
4749
4750 static int
4751 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4752 {
4753 int win = lvalue_p (ref);
4754
4755 if (!win)
4756 lvalue_error (loc, use);
4757
4758 return win;
4759 }
4760 \f
4761 /* Mark EXP saying that we need to be able to take the
4762 address of it; it should not be allocated in a register.
4763 Returns true if successful. ARRAY_REF_P is true if this
4764 is for ARRAY_REF construction - in that case we don't want
4765 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4766 it is fine to use ARRAY_REFs for vector subscripts on vector
4767 register variables. */
4768
4769 bool
4770 c_mark_addressable (tree exp, bool array_ref_p)
4771 {
4772 tree x = exp;
4773
4774 while (1)
4775 switch (TREE_CODE (x))
4776 {
4777 case VIEW_CONVERT_EXPR:
4778 if (array_ref_p
4779 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4780 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4781 return true;
4782 /* FALLTHRU */
4783 case COMPONENT_REF:
4784 case ADDR_EXPR:
4785 case ARRAY_REF:
4786 case REALPART_EXPR:
4787 case IMAGPART_EXPR:
4788 x = TREE_OPERAND (x, 0);
4789 break;
4790
4791 case COMPOUND_LITERAL_EXPR:
4792 case CONSTRUCTOR:
4793 TREE_ADDRESSABLE (x) = 1;
4794 return true;
4795
4796 case VAR_DECL:
4797 case CONST_DECL:
4798 case PARM_DECL:
4799 case RESULT_DECL:
4800 if (C_DECL_REGISTER (x)
4801 && DECL_NONLOCAL (x))
4802 {
4803 if (TREE_PUBLIC (x) || is_global_var (x))
4804 {
4805 error
4806 ("global register variable %qD used in nested function", x);
4807 return false;
4808 }
4809 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4810 }
4811 else if (C_DECL_REGISTER (x))
4812 {
4813 if (TREE_PUBLIC (x) || is_global_var (x))
4814 error ("address of global register variable %qD requested", x);
4815 else
4816 error ("address of register variable %qD requested", x);
4817 return false;
4818 }
4819
4820 /* FALLTHRU */
4821 case FUNCTION_DECL:
4822 TREE_ADDRESSABLE (x) = 1;
4823 /* FALLTHRU */
4824 default:
4825 return true;
4826 }
4827 }
4828 \f
4829 /* Convert EXPR to TYPE, warning about conversion problems with
4830 constants. SEMANTIC_TYPE is the type this conversion would use
4831 without excess precision. If SEMANTIC_TYPE is NULL, this function
4832 is equivalent to convert_and_check. This function is a wrapper that
4833 handles conversions that may be different than
4834 the usual ones because of excess precision. */
4835
4836 static tree
4837 ep_convert_and_check (location_t loc, tree type, tree expr,
4838 tree semantic_type)
4839 {
4840 if (TREE_TYPE (expr) == type)
4841 return expr;
4842
4843 if (!semantic_type)
4844 return convert_and_check (loc, type, expr);
4845
4846 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4847 && TREE_TYPE (expr) != semantic_type)
4848 {
4849 /* For integers, we need to check the real conversion, not
4850 the conversion to the excess precision type. */
4851 expr = convert_and_check (loc, semantic_type, expr);
4852 }
4853 /* Result type is the excess precision type, which should be
4854 large enough, so do not check. */
4855 return convert (type, expr);
4856 }
4857
4858 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4859 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4860 if folded to an integer constant then the unselected half may
4861 contain arbitrary operations not normally permitted in constant
4862 expressions. Set the location of the expression to LOC. */
4863
4864 tree
4865 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4866 tree op1, tree op1_original_type, tree op2,
4867 tree op2_original_type)
4868 {
4869 tree type1;
4870 tree type2;
4871 enum tree_code code1;
4872 enum tree_code code2;
4873 tree result_type = NULL;
4874 tree semantic_result_type = NULL;
4875 tree orig_op1 = op1, orig_op2 = op2;
4876 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4877 bool ifexp_int_operands;
4878 tree ret;
4879
4880 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4881 if (op1_int_operands)
4882 op1 = remove_c_maybe_const_expr (op1);
4883 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4884 if (op2_int_operands)
4885 op2 = remove_c_maybe_const_expr (op2);
4886 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4887 if (ifexp_int_operands)
4888 ifexp = remove_c_maybe_const_expr (ifexp);
4889
4890 /* Promote both alternatives. */
4891
4892 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4893 op1 = default_conversion (op1);
4894 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4895 op2 = default_conversion (op2);
4896
4897 if (TREE_CODE (ifexp) == ERROR_MARK
4898 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4899 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4900 return error_mark_node;
4901
4902 type1 = TREE_TYPE (op1);
4903 code1 = TREE_CODE (type1);
4904 type2 = TREE_TYPE (op2);
4905 code2 = TREE_CODE (type2);
4906
4907 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4908 return error_mark_node;
4909
4910 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4911 return error_mark_node;
4912
4913 /* C90 does not permit non-lvalue arrays in conditional expressions.
4914 In C99 they will be pointers by now. */
4915 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4916 {
4917 error_at (colon_loc, "non-lvalue array in conditional expression");
4918 return error_mark_node;
4919 }
4920
4921 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4922 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4923 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4924 || code1 == COMPLEX_TYPE)
4925 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4926 || code2 == COMPLEX_TYPE))
4927 {
4928 semantic_result_type = c_common_type (type1, type2);
4929 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4930 {
4931 op1 = TREE_OPERAND (op1, 0);
4932 type1 = TREE_TYPE (op1);
4933 gcc_assert (TREE_CODE (type1) == code1);
4934 }
4935 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4936 {
4937 op2 = TREE_OPERAND (op2, 0);
4938 type2 = TREE_TYPE (op2);
4939 gcc_assert (TREE_CODE (type2) == code2);
4940 }
4941 }
4942
4943 if (warn_cxx_compat)
4944 {
4945 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4946 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4947
4948 if (TREE_CODE (t1) == ENUMERAL_TYPE
4949 && TREE_CODE (t2) == ENUMERAL_TYPE
4950 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4951 warning_at (colon_loc, OPT_Wc___compat,
4952 ("different enum types in conditional is "
4953 "invalid in C++: %qT vs %qT"),
4954 t1, t2);
4955 }
4956
4957 /* Quickly detect the usual case where op1 and op2 have the same type
4958 after promotion. */
4959 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4960 {
4961 if (type1 == type2)
4962 result_type = type1;
4963 else
4964 result_type = TYPE_MAIN_VARIANT (type1);
4965 }
4966 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4967 || code1 == COMPLEX_TYPE)
4968 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4969 || code2 == COMPLEX_TYPE))
4970 {
4971 result_type = c_common_type (type1, type2);
4972 if (result_type == error_mark_node)
4973 return error_mark_node;
4974 do_warn_double_promotion (result_type, type1, type2,
4975 "implicit conversion from %qT to %qT to "
4976 "match other result of conditional",
4977 colon_loc);
4978
4979 /* If -Wsign-compare, warn here if type1 and type2 have
4980 different signedness. We'll promote the signed to unsigned
4981 and later code won't know it used to be different.
4982 Do this check on the original types, so that explicit casts
4983 will be considered, but default promotions won't. */
4984 if (c_inhibit_evaluation_warnings == 0)
4985 {
4986 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4987 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4988
4989 if (unsigned_op1 ^ unsigned_op2)
4990 {
4991 bool ovf;
4992
4993 /* Do not warn if the result type is signed, since the
4994 signed type will only be chosen if it can represent
4995 all the values of the unsigned type. */
4996 if (!TYPE_UNSIGNED (result_type))
4997 /* OK */;
4998 else
4999 {
5000 bool op1_maybe_const = true;
5001 bool op2_maybe_const = true;
5002
5003 /* Do not warn if the signed quantity is an
5004 unsuffixed integer literal (or some static
5005 constant expression involving such literals) and
5006 it is non-negative. This warning requires the
5007 operands to be folded for best results, so do
5008 that folding in this case even without
5009 warn_sign_compare to avoid warning options
5010 possibly affecting code generation. */
5011 c_inhibit_evaluation_warnings
5012 += (ifexp == truthvalue_false_node);
5013 op1 = c_fully_fold (op1, require_constant_value,
5014 &op1_maybe_const);
5015 c_inhibit_evaluation_warnings
5016 -= (ifexp == truthvalue_false_node);
5017
5018 c_inhibit_evaluation_warnings
5019 += (ifexp == truthvalue_true_node);
5020 op2 = c_fully_fold (op2, require_constant_value,
5021 &op2_maybe_const);
5022 c_inhibit_evaluation_warnings
5023 -= (ifexp == truthvalue_true_node);
5024
5025 if (warn_sign_compare)
5026 {
5027 if ((unsigned_op2
5028 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5029 || (unsigned_op1
5030 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5031 /* OK */;
5032 else
5033 warning_at (colon_loc, OPT_Wsign_compare,
5034 ("signed and unsigned type in "
5035 "conditional expression"));
5036 }
5037 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5038 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5039 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5040 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5041 }
5042 }
5043 }
5044 }
5045 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5046 {
5047 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5048 pedwarn (colon_loc, OPT_Wpedantic,
5049 "ISO C forbids conditional expr with only one void side");
5050 result_type = void_type_node;
5051 }
5052 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5053 {
5054 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5055 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5056 addr_space_t as_common;
5057
5058 if (comp_target_types (colon_loc, type1, type2))
5059 result_type = common_pointer_type (type1, type2);
5060 else if (null_pointer_constant_p (orig_op1))
5061 result_type = type2;
5062 else if (null_pointer_constant_p (orig_op2))
5063 result_type = type1;
5064 else if (!addr_space_superset (as1, as2, &as_common))
5065 {
5066 error_at (colon_loc, "pointers to disjoint address spaces "
5067 "used in conditional expression");
5068 return error_mark_node;
5069 }
5070 else if (VOID_TYPE_P (TREE_TYPE (type1))
5071 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5072 {
5073 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5074 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5075 & ~TYPE_QUALS (TREE_TYPE (type1))))
5076 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5077 "pointer to array loses qualifier "
5078 "in conditional expression");
5079
5080 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5081 pedwarn (colon_loc, OPT_Wpedantic,
5082 "ISO C forbids conditional expr between "
5083 "%<void *%> and function pointer");
5084 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5085 TREE_TYPE (type2)));
5086 }
5087 else if (VOID_TYPE_P (TREE_TYPE (type2))
5088 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5089 {
5090 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5091 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5092 & ~TYPE_QUALS (TREE_TYPE (type2))))
5093 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5094 "pointer to array loses qualifier "
5095 "in conditional expression");
5096
5097 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5098 pedwarn (colon_loc, OPT_Wpedantic,
5099 "ISO C forbids conditional expr between "
5100 "%<void *%> and function pointer");
5101 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5102 TREE_TYPE (type1)));
5103 }
5104 /* Objective-C pointer comparisons are a bit more lenient. */
5105 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5106 result_type = objc_common_type (type1, type2);
5107 else
5108 {
5109 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5110
5111 pedwarn (colon_loc, 0,
5112 "pointer type mismatch in conditional expression");
5113 result_type = build_pointer_type
5114 (build_qualified_type (void_type_node, qual));
5115 }
5116 }
5117 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5118 {
5119 if (!null_pointer_constant_p (orig_op2))
5120 pedwarn (colon_loc, 0,
5121 "pointer/integer type mismatch in conditional expression");
5122 else
5123 {
5124 op2 = null_pointer_node;
5125 }
5126 result_type = type1;
5127 }
5128 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5129 {
5130 if (!null_pointer_constant_p (orig_op1))
5131 pedwarn (colon_loc, 0,
5132 "pointer/integer type mismatch in conditional expression");
5133 else
5134 {
5135 op1 = null_pointer_node;
5136 }
5137 result_type = type2;
5138 }
5139
5140 if (!result_type)
5141 {
5142 if (flag_cond_mismatch)
5143 result_type = void_type_node;
5144 else
5145 {
5146 error_at (colon_loc, "type mismatch in conditional expression");
5147 return error_mark_node;
5148 }
5149 }
5150
5151 /* Merge const and volatile flags of the incoming types. */
5152 result_type
5153 = build_type_variant (result_type,
5154 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5155 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5156
5157 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5158 semantic_result_type);
5159 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5160 semantic_result_type);
5161
5162 if (ifexp_bcp && ifexp == truthvalue_true_node)
5163 {
5164 op2_int_operands = true;
5165 op1 = c_fully_fold (op1, require_constant_value, NULL);
5166 }
5167 if (ifexp_bcp && ifexp == truthvalue_false_node)
5168 {
5169 op1_int_operands = true;
5170 op2 = c_fully_fold (op2, require_constant_value, NULL);
5171 }
5172 int_const = int_operands = (ifexp_int_operands
5173 && op1_int_operands
5174 && op2_int_operands);
5175 if (int_operands)
5176 {
5177 int_const = ((ifexp == truthvalue_true_node
5178 && TREE_CODE (orig_op1) == INTEGER_CST
5179 && !TREE_OVERFLOW (orig_op1))
5180 || (ifexp == truthvalue_false_node
5181 && TREE_CODE (orig_op2) == INTEGER_CST
5182 && !TREE_OVERFLOW (orig_op2)));
5183 }
5184
5185 /* Need to convert condition operand into a vector mask. */
5186 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5187 {
5188 tree vectype = TREE_TYPE (ifexp);
5189 tree elem_type = TREE_TYPE (vectype);
5190 tree zero = build_int_cst (elem_type, 0);
5191 tree zero_vec = build_vector_from_val (vectype, zero);
5192 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5193 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5194 }
5195
5196 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5197 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5198 else
5199 {
5200 if (int_operands)
5201 {
5202 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5203 nested inside of the expression. */
5204 op1 = c_fully_fold (op1, false, NULL);
5205 op2 = c_fully_fold (op2, false, NULL);
5206 }
5207 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5208 if (int_operands)
5209 ret = note_integer_operands (ret);
5210 }
5211 if (semantic_result_type)
5212 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5213
5214 protected_set_expr_location (ret, colon_loc);
5215
5216 /* If the OP1 and OP2 are the same and don't have side-effects,
5217 warn here, because the COND_EXPR will be turned into OP1. */
5218 if (warn_duplicated_branches
5219 && TREE_CODE (ret) == COND_EXPR
5220 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5221 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5222 "this condition has identical branches");
5223
5224 return ret;
5225 }
5226 \f
5227 /* Return a compound expression that performs two expressions and
5228 returns the value of the second of them.
5229
5230 LOC is the location of the COMPOUND_EXPR. */
5231
5232 tree
5233 build_compound_expr (location_t loc, tree expr1, tree expr2)
5234 {
5235 bool expr1_int_operands, expr2_int_operands;
5236 tree eptype = NULL_TREE;
5237 tree ret;
5238
5239 if (flag_cilkplus
5240 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5241 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5242 {
5243 error_at (loc,
5244 "spawned function call cannot be part of a comma expression");
5245 return error_mark_node;
5246 }
5247 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5248 if (expr1_int_operands)
5249 expr1 = remove_c_maybe_const_expr (expr1);
5250 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5251 if (expr2_int_operands)
5252 expr2 = remove_c_maybe_const_expr (expr2);
5253
5254 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5255 expr1 = TREE_OPERAND (expr1, 0);
5256 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5257 {
5258 eptype = TREE_TYPE (expr2);
5259 expr2 = TREE_OPERAND (expr2, 0);
5260 }
5261
5262 if (!TREE_SIDE_EFFECTS (expr1))
5263 {
5264 /* The left-hand operand of a comma expression is like an expression
5265 statement: with -Wunused, we should warn if it doesn't have
5266 any side-effects, unless it was explicitly cast to (void). */
5267 if (warn_unused_value)
5268 {
5269 if (VOID_TYPE_P (TREE_TYPE (expr1))
5270 && CONVERT_EXPR_P (expr1))
5271 ; /* (void) a, b */
5272 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5273 && TREE_CODE (expr1) == COMPOUND_EXPR
5274 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5275 ; /* (void) a, (void) b, c */
5276 else
5277 warning_at (loc, OPT_Wunused_value,
5278 "left-hand operand of comma expression has no effect");
5279 }
5280 }
5281 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5282 && warn_unused_value)
5283 {
5284 tree r = expr1;
5285 location_t cloc = loc;
5286 while (TREE_CODE (r) == COMPOUND_EXPR)
5287 {
5288 if (EXPR_HAS_LOCATION (r))
5289 cloc = EXPR_LOCATION (r);
5290 r = TREE_OPERAND (r, 1);
5291 }
5292 if (!TREE_SIDE_EFFECTS (r)
5293 && !VOID_TYPE_P (TREE_TYPE (r))
5294 && !CONVERT_EXPR_P (r))
5295 warning_at (cloc, OPT_Wunused_value,
5296 "right-hand operand of comma expression has no effect");
5297 }
5298
5299 /* With -Wunused, we should also warn if the left-hand operand does have
5300 side-effects, but computes a value which is not used. For example, in
5301 `foo() + bar(), baz()' the result of the `+' operator is not used,
5302 so we should issue a warning. */
5303 else if (warn_unused_value)
5304 warn_if_unused_value (expr1, loc);
5305
5306 if (expr2 == error_mark_node)
5307 return error_mark_node;
5308
5309 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5310
5311 if (flag_isoc99
5312 && expr1_int_operands
5313 && expr2_int_operands)
5314 ret = note_integer_operands (ret);
5315
5316 if (eptype)
5317 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5318
5319 protected_set_expr_location (ret, loc);
5320 return ret;
5321 }
5322
5323 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5324 which we are casting. OTYPE is the type of the expression being
5325 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5326 of the cast. -Wcast-qual appeared on the command line. Named
5327 address space qualifiers are not handled here, because they result
5328 in different warnings. */
5329
5330 static void
5331 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5332 {
5333 tree in_type = type;
5334 tree in_otype = otype;
5335 int added = 0;
5336 int discarded = 0;
5337 bool is_const;
5338
5339 /* Check that the qualifiers on IN_TYPE are a superset of the
5340 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5341 nodes is uninteresting and we stop as soon as we hit a
5342 non-POINTER_TYPE node on either type. */
5343 do
5344 {
5345 in_otype = TREE_TYPE (in_otype);
5346 in_type = TREE_TYPE (in_type);
5347
5348 /* GNU C allows cv-qualified function types. 'const' means the
5349 function is very pure, 'volatile' means it can't return. We
5350 need to warn when such qualifiers are added, not when they're
5351 taken away. */
5352 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5353 && TREE_CODE (in_type) == FUNCTION_TYPE)
5354 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5355 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5356 else
5357 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5358 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5359 }
5360 while (TREE_CODE (in_type) == POINTER_TYPE
5361 && TREE_CODE (in_otype) == POINTER_TYPE);
5362
5363 if (added)
5364 warning_at (loc, OPT_Wcast_qual,
5365 "cast adds %q#v qualifier to function type", added);
5366
5367 if (discarded)
5368 /* There are qualifiers present in IN_OTYPE that are not present
5369 in IN_TYPE. */
5370 warning_at (loc, OPT_Wcast_qual,
5371 "cast discards %qv qualifier from pointer target type",
5372 discarded);
5373
5374 if (added || discarded)
5375 return;
5376
5377 /* A cast from **T to const **T is unsafe, because it can cause a
5378 const value to be changed with no additional warning. We only
5379 issue this warning if T is the same on both sides, and we only
5380 issue the warning if there are the same number of pointers on
5381 both sides, as otherwise the cast is clearly unsafe anyhow. A
5382 cast is unsafe when a qualifier is added at one level and const
5383 is not present at all outer levels.
5384
5385 To issue this warning, we check at each level whether the cast
5386 adds new qualifiers not already seen. We don't need to special
5387 case function types, as they won't have the same
5388 TYPE_MAIN_VARIANT. */
5389
5390 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5391 return;
5392 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5393 return;
5394
5395 in_type = type;
5396 in_otype = otype;
5397 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5398 do
5399 {
5400 in_type = TREE_TYPE (in_type);
5401 in_otype = TREE_TYPE (in_otype);
5402 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5403 && !is_const)
5404 {
5405 warning_at (loc, OPT_Wcast_qual,
5406 "to be safe all intermediate pointers in cast from "
5407 "%qT to %qT must be %<const%> qualified",
5408 otype, type);
5409 break;
5410 }
5411 if (is_const)
5412 is_const = TYPE_READONLY (in_type);
5413 }
5414 while (TREE_CODE (in_type) == POINTER_TYPE);
5415 }
5416
5417 /* Build an expression representing a cast to type TYPE of expression EXPR.
5418 LOC is the location of the cast-- typically the open paren of the cast. */
5419
5420 tree
5421 build_c_cast (location_t loc, tree type, tree expr)
5422 {
5423 tree value;
5424
5425 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5426 expr = TREE_OPERAND (expr, 0);
5427
5428 value = expr;
5429
5430 if (type == error_mark_node || expr == error_mark_node)
5431 return error_mark_node;
5432
5433 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5434 only in <protocol> qualifications. But when constructing cast expressions,
5435 the protocols do matter and must be kept around. */
5436 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5437 return build1 (NOP_EXPR, type, expr);
5438
5439 type = TYPE_MAIN_VARIANT (type);
5440
5441 if (TREE_CODE (type) == ARRAY_TYPE)
5442 {
5443 error_at (loc, "cast specifies array type");
5444 return error_mark_node;
5445 }
5446
5447 if (TREE_CODE (type) == FUNCTION_TYPE)
5448 {
5449 error_at (loc, "cast specifies function type");
5450 return error_mark_node;
5451 }
5452
5453 if (!VOID_TYPE_P (type))
5454 {
5455 value = require_complete_type (loc, value);
5456 if (value == error_mark_node)
5457 return error_mark_node;
5458 }
5459
5460 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5461 {
5462 if (RECORD_OR_UNION_TYPE_P (type))
5463 pedwarn (loc, OPT_Wpedantic,
5464 "ISO C forbids casting nonscalar to the same type");
5465
5466 /* Convert to remove any qualifiers from VALUE's type. */
5467 value = convert (type, value);
5468 }
5469 else if (TREE_CODE (type) == UNION_TYPE)
5470 {
5471 tree field;
5472
5473 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5474 if (TREE_TYPE (field) != error_mark_node
5475 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5476 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5477 break;
5478
5479 if (field)
5480 {
5481 tree t;
5482 bool maybe_const = true;
5483
5484 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5485 t = c_fully_fold (value, false, &maybe_const);
5486 t = build_constructor_single (type, field, t);
5487 if (!maybe_const)
5488 t = c_wrap_maybe_const (t, true);
5489 t = digest_init (loc, type, t,
5490 NULL_TREE, false, true, 0);
5491 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5492 return t;
5493 }
5494 error_at (loc, "cast to union type from type not present in union");
5495 return error_mark_node;
5496 }
5497 else
5498 {
5499 tree otype, ovalue;
5500
5501 if (type == void_type_node)
5502 {
5503 tree t = build1 (CONVERT_EXPR, type, value);
5504 SET_EXPR_LOCATION (t, loc);
5505 return t;
5506 }
5507
5508 otype = TREE_TYPE (value);
5509
5510 /* Optionally warn about potentially worrisome casts. */
5511 if (warn_cast_qual
5512 && TREE_CODE (type) == POINTER_TYPE
5513 && TREE_CODE (otype) == POINTER_TYPE)
5514 handle_warn_cast_qual (loc, type, otype);
5515
5516 /* Warn about conversions between pointers to disjoint
5517 address spaces. */
5518 if (TREE_CODE (type) == POINTER_TYPE
5519 && TREE_CODE (otype) == POINTER_TYPE
5520 && !null_pointer_constant_p (value))
5521 {
5522 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5523 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5524 addr_space_t as_common;
5525
5526 if (!addr_space_superset (as_to, as_from, &as_common))
5527 {
5528 if (ADDR_SPACE_GENERIC_P (as_from))
5529 warning_at (loc, 0, "cast to %s address space pointer "
5530 "from disjoint generic address space pointer",
5531 c_addr_space_name (as_to));
5532
5533 else if (ADDR_SPACE_GENERIC_P (as_to))
5534 warning_at (loc, 0, "cast to generic address space pointer "
5535 "from disjoint %s address space pointer",
5536 c_addr_space_name (as_from));
5537
5538 else
5539 warning_at (loc, 0, "cast to %s address space pointer "
5540 "from disjoint %s address space pointer",
5541 c_addr_space_name (as_to),
5542 c_addr_space_name (as_from));
5543 }
5544 }
5545
5546 /* Warn about possible alignment problems. */
5547 if (STRICT_ALIGNMENT
5548 && TREE_CODE (type) == POINTER_TYPE
5549 && TREE_CODE (otype) == POINTER_TYPE
5550 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5551 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5552 /* Don't warn about opaque types, where the actual alignment
5553 restriction is unknown. */
5554 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5555 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5556 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5557 warning_at (loc, OPT_Wcast_align,
5558 "cast increases required alignment of target type");
5559
5560 if (TREE_CODE (type) == INTEGER_TYPE
5561 && TREE_CODE (otype) == POINTER_TYPE
5562 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5563 /* Unlike conversion of integers to pointers, where the
5564 warning is disabled for converting constants because
5565 of cases such as SIG_*, warn about converting constant
5566 pointers to integers. In some cases it may cause unwanted
5567 sign extension, and a warning is appropriate. */
5568 warning_at (loc, OPT_Wpointer_to_int_cast,
5569 "cast from pointer to integer of different size");
5570
5571 if (TREE_CODE (value) == CALL_EXPR
5572 && TREE_CODE (type) != TREE_CODE (otype))
5573 warning_at (loc, OPT_Wbad_function_cast,
5574 "cast from function call of type %qT "
5575 "to non-matching type %qT", otype, type);
5576
5577 if (TREE_CODE (type) == POINTER_TYPE
5578 && TREE_CODE (otype) == INTEGER_TYPE
5579 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5580 /* Don't warn about converting any constant. */
5581 && !TREE_CONSTANT (value))
5582 warning_at (loc,
5583 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5584 "of different size");
5585
5586 if (warn_strict_aliasing <= 2)
5587 strict_aliasing_warning (otype, type, expr);
5588
5589 /* If pedantic, warn for conversions between function and object
5590 pointer types, except for converting a null pointer constant
5591 to function pointer type. */
5592 if (pedantic
5593 && TREE_CODE (type) == POINTER_TYPE
5594 && TREE_CODE (otype) == POINTER_TYPE
5595 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5596 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5597 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5598 "conversion of function pointer to object pointer type");
5599
5600 if (pedantic
5601 && TREE_CODE (type) == POINTER_TYPE
5602 && TREE_CODE (otype) == POINTER_TYPE
5603 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5604 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5605 && !null_pointer_constant_p (value))
5606 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5607 "conversion of object pointer to function pointer type");
5608
5609 ovalue = value;
5610 value = convert (type, value);
5611
5612 /* Ignore any integer overflow caused by the cast. */
5613 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5614 {
5615 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5616 {
5617 if (!TREE_OVERFLOW (value))
5618 {
5619 /* Avoid clobbering a shared constant. */
5620 value = copy_node (value);
5621 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5622 }
5623 }
5624 else if (TREE_OVERFLOW (value))
5625 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5626 value = wide_int_to_tree (TREE_TYPE (value), value);
5627 }
5628 }
5629
5630 /* Don't let a cast be an lvalue. */
5631 if (lvalue_p (value))
5632 value = non_lvalue_loc (loc, value);
5633
5634 /* Don't allow the results of casting to floating-point or complex
5635 types be confused with actual constants, or casts involving
5636 integer and pointer types other than direct integer-to-integer
5637 and integer-to-pointer be confused with integer constant
5638 expressions and null pointer constants. */
5639 if (TREE_CODE (value) == REAL_CST
5640 || TREE_CODE (value) == COMPLEX_CST
5641 || (TREE_CODE (value) == INTEGER_CST
5642 && !((TREE_CODE (expr) == INTEGER_CST
5643 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5644 || TREE_CODE (expr) == REAL_CST
5645 || TREE_CODE (expr) == COMPLEX_CST)))
5646 value = build1 (NOP_EXPR, type, value);
5647
5648 protected_set_expr_location (value, loc);
5649 return value;
5650 }
5651
5652 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5653 location of the open paren of the cast, or the position of the cast
5654 expr. */
5655 tree
5656 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5657 {
5658 tree type;
5659 tree type_expr = NULL_TREE;
5660 bool type_expr_const = true;
5661 tree ret;
5662 int saved_wsp = warn_strict_prototypes;
5663
5664 /* This avoids warnings about unprototyped casts on
5665 integers. E.g. "#define SIG_DFL (void(*)())0". */
5666 if (TREE_CODE (expr) == INTEGER_CST)
5667 warn_strict_prototypes = 0;
5668 type = groktypename (type_name, &type_expr, &type_expr_const);
5669 warn_strict_prototypes = saved_wsp;
5670
5671 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5672 && reject_gcc_builtin (expr))
5673 return error_mark_node;
5674
5675 ret = build_c_cast (loc, type, expr);
5676 if (type_expr)
5677 {
5678 bool inner_expr_const = true;
5679 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5680 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5681 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5682 && inner_expr_const);
5683 SET_EXPR_LOCATION (ret, loc);
5684 }
5685
5686 if (!EXPR_HAS_LOCATION (ret))
5687 protected_set_expr_location (ret, loc);
5688
5689 /* C++ does not permits types to be defined in a cast, but it
5690 allows references to incomplete types. */
5691 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5692 warning_at (loc, OPT_Wc___compat,
5693 "defining a type in a cast is invalid in C++");
5694
5695 return ret;
5696 }
5697 \f
5698 /* Build an assignment expression of lvalue LHS from value RHS.
5699 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5700 may differ from TREE_TYPE (LHS) for an enum bitfield.
5701 MODIFYCODE is the code for a binary operator that we use
5702 to combine the old value of LHS with RHS to get the new value.
5703 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5704 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5705 which may differ from TREE_TYPE (RHS) for an enum value.
5706
5707 LOCATION is the location of the MODIFYCODE operator.
5708 RHS_LOC is the location of the RHS. */
5709
5710 tree
5711 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5712 enum tree_code modifycode,
5713 location_t rhs_loc, tree rhs, tree rhs_origtype)
5714 {
5715 tree result;
5716 tree newrhs;
5717 tree rhseval = NULL_TREE;
5718 tree rhs_semantic_type = NULL_TREE;
5719 tree lhstype = TREE_TYPE (lhs);
5720 tree olhstype = lhstype;
5721 bool npc;
5722 bool is_atomic_op;
5723
5724 /* Types that aren't fully specified cannot be used in assignments. */
5725 lhs = require_complete_type (location, lhs);
5726
5727 /* Avoid duplicate error messages from operands that had errors. */
5728 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5729 return error_mark_node;
5730
5731 /* Ensure an error for assigning a non-lvalue array to an array in
5732 C90. */
5733 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5734 {
5735 error_at (location, "assignment to expression with array type");
5736 return error_mark_node;
5737 }
5738
5739 /* For ObjC properties, defer this check. */
5740 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5741 return error_mark_node;
5742
5743 is_atomic_op = really_atomic_lvalue (lhs);
5744
5745 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5746 {
5747 rhs_semantic_type = TREE_TYPE (rhs);
5748 rhs = TREE_OPERAND (rhs, 0);
5749 }
5750
5751 newrhs = rhs;
5752
5753 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5754 {
5755 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5756 lhs_origtype, modifycode, rhs_loc, rhs,
5757 rhs_origtype);
5758 if (inner == error_mark_node)
5759 return error_mark_node;
5760 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5761 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5762 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5763 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5764 protected_set_expr_location (result, location);
5765 return result;
5766 }
5767
5768 /* If a binary op has been requested, combine the old LHS value with the RHS
5769 producing the value we should actually store into the LHS. */
5770
5771 if (modifycode != NOP_EXPR)
5772 {
5773 lhs = c_fully_fold (lhs, false, NULL);
5774 lhs = stabilize_reference (lhs);
5775
5776 /* Construct the RHS for any non-atomic compound assignemnt. */
5777 if (!is_atomic_op)
5778 {
5779 /* If in LHS op= RHS the RHS has side-effects, ensure they
5780 are preevaluated before the rest of the assignment expression's
5781 side-effects, because RHS could contain e.g. function calls
5782 that modify LHS. */
5783 if (TREE_SIDE_EFFECTS (rhs))
5784 {
5785 newrhs = save_expr (rhs);
5786 rhseval = newrhs;
5787 }
5788 newrhs = build_binary_op (location,
5789 modifycode, lhs, newrhs, 1);
5790
5791 /* The original type of the right hand side is no longer
5792 meaningful. */
5793 rhs_origtype = NULL_TREE;
5794 }
5795 }
5796
5797 if (c_dialect_objc ())
5798 {
5799 /* Check if we are modifying an Objective-C property reference;
5800 if so, we need to generate setter calls. */
5801 result = objc_maybe_build_modify_expr (lhs, newrhs);
5802 if (result)
5803 goto return_result;
5804
5805 /* Else, do the check that we postponed for Objective-C. */
5806 if (!lvalue_or_else (location, lhs, lv_assign))
5807 return error_mark_node;
5808 }
5809
5810 /* Give an error for storing in something that is 'const'. */
5811
5812 if (TYPE_READONLY (lhstype)
5813 || (RECORD_OR_UNION_TYPE_P (lhstype)
5814 && C_TYPE_FIELDS_READONLY (lhstype)))
5815 {
5816 readonly_error (location, lhs, lv_assign);
5817 return error_mark_node;
5818 }
5819 else if (TREE_READONLY (lhs))
5820 readonly_warning (lhs, lv_assign);
5821
5822 /* If storing into a structure or union member,
5823 it has probably been given type `int'.
5824 Compute the type that would go with
5825 the actual amount of storage the member occupies. */
5826
5827 if (TREE_CODE (lhs) == COMPONENT_REF
5828 && (TREE_CODE (lhstype) == INTEGER_TYPE
5829 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5830 || TREE_CODE (lhstype) == REAL_TYPE
5831 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5832 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5833
5834 /* If storing in a field that is in actuality a short or narrower than one,
5835 we must store in the field in its actual type. */
5836
5837 if (lhstype != TREE_TYPE (lhs))
5838 {
5839 lhs = copy_node (lhs);
5840 TREE_TYPE (lhs) = lhstype;
5841 }
5842
5843 /* Issue -Wc++-compat warnings about an assignment to an enum type
5844 when LHS does not have its original type. This happens for,
5845 e.g., an enum bitfield in a struct. */
5846 if (warn_cxx_compat
5847 && lhs_origtype != NULL_TREE
5848 && lhs_origtype != lhstype
5849 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5850 {
5851 tree checktype = (rhs_origtype != NULL_TREE
5852 ? rhs_origtype
5853 : TREE_TYPE (rhs));
5854 if (checktype != error_mark_node
5855 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5856 || (is_atomic_op && modifycode != NOP_EXPR)))
5857 warning_at (location, OPT_Wc___compat,
5858 "enum conversion in assignment is invalid in C++");
5859 }
5860
5861 /* If the lhs is atomic, remove that qualifier. */
5862 if (is_atomic_op)
5863 {
5864 lhstype = build_qualified_type (lhstype,
5865 (TYPE_QUALS (lhstype)
5866 & ~TYPE_QUAL_ATOMIC));
5867 olhstype = build_qualified_type (olhstype,
5868 (TYPE_QUALS (lhstype)
5869 & ~TYPE_QUAL_ATOMIC));
5870 }
5871
5872 /* Convert new value to destination type. Fold it first, then
5873 restore any excess precision information, for the sake of
5874 conversion warnings. */
5875
5876 if (!(is_atomic_op && modifycode != NOP_EXPR))
5877 {
5878 npc = null_pointer_constant_p (newrhs);
5879 newrhs = c_fully_fold (newrhs, false, NULL);
5880 if (rhs_semantic_type)
5881 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5882 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5883 rhs_origtype, ic_assign, npc,
5884 NULL_TREE, NULL_TREE, 0);
5885 if (TREE_CODE (newrhs) == ERROR_MARK)
5886 return error_mark_node;
5887 }
5888
5889 /* Emit ObjC write barrier, if necessary. */
5890 if (c_dialect_objc () && flag_objc_gc)
5891 {
5892 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5893 if (result)
5894 {
5895 protected_set_expr_location (result, location);
5896 goto return_result;
5897 }
5898 }
5899
5900 /* Scan operands. */
5901
5902 if (is_atomic_op)
5903 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5904 else
5905 {
5906 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5907 TREE_SIDE_EFFECTS (result) = 1;
5908 protected_set_expr_location (result, location);
5909 }
5910
5911 /* If we got the LHS in a different type for storing in,
5912 convert the result back to the nominal type of LHS
5913 so that the value we return always has the same type
5914 as the LHS argument. */
5915
5916 if (olhstype == TREE_TYPE (result))
5917 goto return_result;
5918
5919 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5920 rhs_origtype, ic_assign, false, NULL_TREE,
5921 NULL_TREE, 0);
5922 protected_set_expr_location (result, location);
5923
5924 return_result:
5925 if (rhseval)
5926 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5927 return result;
5928 }
5929 \f
5930 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5931 This is used to implement -fplan9-extensions. */
5932
5933 static bool
5934 find_anonymous_field_with_type (tree struct_type, tree type)
5935 {
5936 tree field;
5937 bool found;
5938
5939 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
5940 found = false;
5941 for (field = TYPE_FIELDS (struct_type);
5942 field != NULL_TREE;
5943 field = TREE_CHAIN (field))
5944 {
5945 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5946 ? c_build_qualified_type (TREE_TYPE (field),
5947 TYPE_QUAL_ATOMIC)
5948 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5949 if (DECL_NAME (field) == NULL
5950 && comptypes (type, fieldtype))
5951 {
5952 if (found)
5953 return false;
5954 found = true;
5955 }
5956 else if (DECL_NAME (field) == NULL
5957 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
5958 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5959 {
5960 if (found)
5961 return false;
5962 found = true;
5963 }
5964 }
5965 return found;
5966 }
5967
5968 /* RHS is an expression whose type is pointer to struct. If there is
5969 an anonymous field in RHS with type TYPE, then return a pointer to
5970 that field in RHS. This is used with -fplan9-extensions. This
5971 returns NULL if no conversion could be found. */
5972
5973 static tree
5974 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5975 {
5976 tree rhs_struct_type, lhs_main_type;
5977 tree field, found_field;
5978 bool found_sub_field;
5979 tree ret;
5980
5981 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5982 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5983 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
5984
5985 gcc_assert (POINTER_TYPE_P (type));
5986 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5987 ? c_build_qualified_type (TREE_TYPE (type),
5988 TYPE_QUAL_ATOMIC)
5989 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5990
5991 found_field = NULL_TREE;
5992 found_sub_field = false;
5993 for (field = TYPE_FIELDS (rhs_struct_type);
5994 field != NULL_TREE;
5995 field = TREE_CHAIN (field))
5996 {
5997 if (DECL_NAME (field) != NULL_TREE
5998 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
5999 continue;
6000 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6001 ? c_build_qualified_type (TREE_TYPE (field),
6002 TYPE_QUAL_ATOMIC)
6003 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6004 if (comptypes (lhs_main_type, fieldtype))
6005 {
6006 if (found_field != NULL_TREE)
6007 return NULL_TREE;
6008 found_field = field;
6009 }
6010 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6011 lhs_main_type))
6012 {
6013 if (found_field != NULL_TREE)
6014 return NULL_TREE;
6015 found_field = field;
6016 found_sub_field = true;
6017 }
6018 }
6019
6020 if (found_field == NULL_TREE)
6021 return NULL_TREE;
6022
6023 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6024 build_fold_indirect_ref (rhs), found_field,
6025 NULL_TREE);
6026 ret = build_fold_addr_expr_loc (location, ret);
6027
6028 if (found_sub_field)
6029 {
6030 ret = convert_to_anonymous_field (location, type, ret);
6031 gcc_assert (ret != NULL_TREE);
6032 }
6033
6034 return ret;
6035 }
6036
6037 /* Issue an error message for a bad initializer component.
6038 GMSGID identifies the message.
6039 The component name is taken from the spelling stack. */
6040
6041 static void
6042 error_init (location_t loc, const char *gmsgid)
6043 {
6044 char *ofwhat;
6045
6046 /* The gmsgid may be a format string with %< and %>. */
6047 error_at (loc, gmsgid);
6048 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6049 if (*ofwhat)
6050 inform (loc, "(near initialization for %qs)", ofwhat);
6051 }
6052
6053 /* Issue a pedantic warning for a bad initializer component. OPT is
6054 the option OPT_* (from options.h) controlling this warning or 0 if
6055 it is unconditionally given. GMSGID identifies the message. The
6056 component name is taken from the spelling stack. */
6057
6058 static void
6059 pedwarn_init (location_t loc, int opt, const char *gmsgid)
6060 {
6061 char *ofwhat;
6062 bool warned;
6063
6064 /* Use the location where a macro was expanded rather than where
6065 it was defined to make sure macros defined in system headers
6066 but used incorrectly elsewhere are diagnosed. */
6067 source_location exploc = expansion_point_location_if_in_system_header (loc);
6068
6069 /* The gmsgid may be a format string with %< and %>. */
6070 warned = pedwarn (exploc, opt, gmsgid);
6071 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6072 if (*ofwhat && warned)
6073 inform (exploc, "(near initialization for %qs)", ofwhat);
6074 }
6075
6076 /* Issue a warning for a bad initializer component.
6077
6078 OPT is the OPT_W* value corresponding to the warning option that
6079 controls this warning. GMSGID identifies the message. The
6080 component name is taken from the spelling stack. */
6081
6082 static void
6083 warning_init (location_t loc, int opt, const char *gmsgid)
6084 {
6085 char *ofwhat;
6086 bool warned;
6087
6088 /* Use the location where a macro was expanded rather than where
6089 it was defined to make sure macros defined in system headers
6090 but used incorrectly elsewhere are diagnosed. */
6091 source_location exploc = expansion_point_location_if_in_system_header (loc);
6092
6093 /* The gmsgid may be a format string with %< and %>. */
6094 warned = warning_at (exploc, opt, gmsgid);
6095 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6096 if (*ofwhat && warned)
6097 inform (exploc, "(near initialization for %qs)", ofwhat);
6098 }
6099 \f
6100 /* If TYPE is an array type and EXPR is a parenthesized string
6101 constant, warn if pedantic that EXPR is being used to initialize an
6102 object of type TYPE. */
6103
6104 void
6105 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6106 {
6107 if (pedantic
6108 && TREE_CODE (type) == ARRAY_TYPE
6109 && TREE_CODE (expr.value) == STRING_CST
6110 && expr.original_code != STRING_CST)
6111 pedwarn_init (loc, OPT_Wpedantic,
6112 "array initialized from parenthesized string constant");
6113 }
6114
6115 /* Convert value RHS to type TYPE as preparation for an assignment to
6116 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6117 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6118 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6119 constant before any folding.
6120 The real work of conversion is done by `convert'.
6121 The purpose of this function is to generate error messages
6122 for assignments that are not allowed in C.
6123 ERRTYPE says whether it is argument passing, assignment,
6124 initialization or return.
6125
6126 In the following example, '~' denotes where EXPR_LOC and '^' where
6127 LOCATION point to:
6128
6129 f (var); [ic_argpass]
6130 ^ ~~~
6131 x = var; [ic_assign]
6132 ^ ~~~;
6133 int x = var; [ic_init]
6134 ^^^
6135 return x; [ic_return]
6136 ^
6137
6138 FUNCTION is a tree for the function being called.
6139 PARMNUM is the number of the argument, for printing in error messages. */
6140
6141 static tree
6142 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6143 tree rhs, tree origtype, enum impl_conv errtype,
6144 bool null_pointer_constant, tree fundecl,
6145 tree function, int parmnum)
6146 {
6147 enum tree_code codel = TREE_CODE (type);
6148 tree orig_rhs = rhs;
6149 tree rhstype;
6150 enum tree_code coder;
6151 tree rname = NULL_TREE;
6152 bool objc_ok = false;
6153
6154 /* Use the expansion point location to handle cases such as user's
6155 function returning a wrong-type macro defined in a system header. */
6156 location = expansion_point_location_if_in_system_header (location);
6157
6158 if (errtype == ic_argpass)
6159 {
6160 tree selector;
6161 /* Change pointer to function to the function itself for
6162 diagnostics. */
6163 if (TREE_CODE (function) == ADDR_EXPR
6164 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6165 function = TREE_OPERAND (function, 0);
6166
6167 /* Handle an ObjC selector specially for diagnostics. */
6168 selector = objc_message_selector ();
6169 rname = function;
6170 if (selector && parmnum > 2)
6171 {
6172 rname = selector;
6173 parmnum -= 2;
6174 }
6175 }
6176
6177 /* This macro is used to emit diagnostics to ensure that all format
6178 strings are complete sentences, visible to gettext and checked at
6179 compile time. */
6180 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6181 do { \
6182 switch (errtype) \
6183 { \
6184 case ic_argpass: \
6185 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6186 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6187 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6188 "expected %qT but argument is of type %qT", \
6189 type, rhstype); \
6190 break; \
6191 case ic_assign: \
6192 pedwarn (LOCATION, OPT, AS); \
6193 break; \
6194 case ic_init: \
6195 pedwarn_init (LOCATION, OPT, IN); \
6196 break; \
6197 case ic_return: \
6198 pedwarn (LOCATION, OPT, RE); \
6199 break; \
6200 default: \
6201 gcc_unreachable (); \
6202 } \
6203 } while (0)
6204
6205 /* This macro is used to emit diagnostics to ensure that all format
6206 strings are complete sentences, visible to gettext and checked at
6207 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6208 extra parameter to enumerate qualifiers. */
6209 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6210 do { \
6211 switch (errtype) \
6212 { \
6213 case ic_argpass: \
6214 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6215 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6216 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6217 "expected %qT but argument is of type %qT", \
6218 type, rhstype); \
6219 break; \
6220 case ic_assign: \
6221 pedwarn (LOCATION, OPT, AS, QUALS); \
6222 break; \
6223 case ic_init: \
6224 pedwarn (LOCATION, OPT, IN, QUALS); \
6225 break; \
6226 case ic_return: \
6227 pedwarn (LOCATION, OPT, RE, QUALS); \
6228 break; \
6229 default: \
6230 gcc_unreachable (); \
6231 } \
6232 } while (0)
6233
6234 /* This macro is used to emit diagnostics to ensure that all format
6235 strings are complete sentences, visible to gettext and checked at
6236 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6237 warning_at instead of pedwarn. */
6238 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6239 do { \
6240 switch (errtype) \
6241 { \
6242 case ic_argpass: \
6243 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6244 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6245 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6246 "expected %qT but argument is of type %qT", \
6247 type, rhstype); \
6248 break; \
6249 case ic_assign: \
6250 warning_at (LOCATION, OPT, AS, QUALS); \
6251 break; \
6252 case ic_init: \
6253 warning_at (LOCATION, OPT, IN, QUALS); \
6254 break; \
6255 case ic_return: \
6256 warning_at (LOCATION, OPT, RE, QUALS); \
6257 break; \
6258 default: \
6259 gcc_unreachable (); \
6260 } \
6261 } while (0)
6262
6263 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6264 rhs = TREE_OPERAND (rhs, 0);
6265
6266 rhstype = TREE_TYPE (rhs);
6267 coder = TREE_CODE (rhstype);
6268
6269 if (coder == ERROR_MARK)
6270 return error_mark_node;
6271
6272 if (c_dialect_objc ())
6273 {
6274 int parmno;
6275
6276 switch (errtype)
6277 {
6278 case ic_return:
6279 parmno = 0;
6280 break;
6281
6282 case ic_assign:
6283 parmno = -1;
6284 break;
6285
6286 case ic_init:
6287 parmno = -2;
6288 break;
6289
6290 default:
6291 parmno = parmnum;
6292 break;
6293 }
6294
6295 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6296 }
6297
6298 if (warn_cxx_compat)
6299 {
6300 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6301 if (checktype != error_mark_node
6302 && TREE_CODE (type) == ENUMERAL_TYPE
6303 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6304 {
6305 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
6306 G_("enum conversion when passing argument "
6307 "%d of %qE is invalid in C++"),
6308 G_("enum conversion in assignment is "
6309 "invalid in C++"),
6310 G_("enum conversion in initialization is "
6311 "invalid in C++"),
6312 G_("enum conversion in return is "
6313 "invalid in C++"));
6314 }
6315 }
6316
6317 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6318 return rhs;
6319
6320 if (coder == VOID_TYPE)
6321 {
6322 /* Except for passing an argument to an unprototyped function,
6323 this is a constraint violation. When passing an argument to
6324 an unprototyped function, it is compile-time undefined;
6325 making it a constraint in that case was rejected in
6326 DR#252. */
6327 error_at (location, "void value not ignored as it ought to be");
6328 return error_mark_node;
6329 }
6330 rhs = require_complete_type (location, rhs);
6331 if (rhs == error_mark_node)
6332 return error_mark_node;
6333
6334 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6335 return error_mark_node;
6336
6337 /* A non-reference type can convert to a reference. This handles
6338 va_start, va_copy and possibly port built-ins. */
6339 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6340 {
6341 if (!lvalue_p (rhs))
6342 {
6343 error_at (location, "cannot pass rvalue to reference parameter");
6344 return error_mark_node;
6345 }
6346 if (!c_mark_addressable (rhs))
6347 return error_mark_node;
6348 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6349 SET_EXPR_LOCATION (rhs, location);
6350
6351 rhs = convert_for_assignment (location, expr_loc,
6352 build_pointer_type (TREE_TYPE (type)),
6353 rhs, origtype, errtype,
6354 null_pointer_constant, fundecl, function,
6355 parmnum);
6356 if (rhs == error_mark_node)
6357 return error_mark_node;
6358
6359 rhs = build1 (NOP_EXPR, type, rhs);
6360 SET_EXPR_LOCATION (rhs, location);
6361 return rhs;
6362 }
6363 /* Some types can interconvert without explicit casts. */
6364 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6365 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6366 return convert (type, rhs);
6367 /* Arithmetic types all interconvert, and enum is treated like int. */
6368 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6369 || codel == FIXED_POINT_TYPE
6370 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6371 || codel == BOOLEAN_TYPE)
6372 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6373 || coder == FIXED_POINT_TYPE
6374 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6375 || coder == BOOLEAN_TYPE))
6376 {
6377 tree ret;
6378 bool save = in_late_binary_op;
6379 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6380 || (coder == REAL_TYPE
6381 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6382 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6383 in_late_binary_op = true;
6384 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6385 ? expr_loc : location, type, orig_rhs);
6386 in_late_binary_op = save;
6387 return ret;
6388 }
6389
6390 /* Aggregates in different TUs might need conversion. */
6391 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6392 && codel == coder
6393 && comptypes (type, rhstype))
6394 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6395 ? expr_loc : location, type, rhs);
6396
6397 /* Conversion to a transparent union or record from its member types.
6398 This applies only to function arguments. */
6399 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6400 && TYPE_TRANSPARENT_AGGR (type))
6401 && errtype == ic_argpass)
6402 {
6403 tree memb, marginal_memb = NULL_TREE;
6404
6405 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6406 {
6407 tree memb_type = TREE_TYPE (memb);
6408
6409 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6410 TYPE_MAIN_VARIANT (rhstype)))
6411 break;
6412
6413 if (TREE_CODE (memb_type) != POINTER_TYPE)
6414 continue;
6415
6416 if (coder == POINTER_TYPE)
6417 {
6418 tree ttl = TREE_TYPE (memb_type);
6419 tree ttr = TREE_TYPE (rhstype);
6420
6421 /* Any non-function converts to a [const][volatile] void *
6422 and vice versa; otherwise, targets must be the same.
6423 Meanwhile, the lhs target must have all the qualifiers of
6424 the rhs. */
6425 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6426 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6427 || comp_target_types (location, memb_type, rhstype))
6428 {
6429 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6430 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6431 /* If this type won't generate any warnings, use it. */
6432 if (lquals == rquals
6433 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6434 && TREE_CODE (ttl) == FUNCTION_TYPE)
6435 ? ((lquals | rquals) == rquals)
6436 : ((lquals | rquals) == lquals)))
6437 break;
6438
6439 /* Keep looking for a better type, but remember this one. */
6440 if (!marginal_memb)
6441 marginal_memb = memb;
6442 }
6443 }
6444
6445 /* Can convert integer zero to any pointer type. */
6446 if (null_pointer_constant)
6447 {
6448 rhs = null_pointer_node;
6449 break;
6450 }
6451 }
6452
6453 if (memb || marginal_memb)
6454 {
6455 if (!memb)
6456 {
6457 /* We have only a marginally acceptable member type;
6458 it needs a warning. */
6459 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6460 tree ttr = TREE_TYPE (rhstype);
6461
6462 /* Const and volatile mean something different for function
6463 types, so the usual warnings are not appropriate. */
6464 if (TREE_CODE (ttr) == FUNCTION_TYPE
6465 && TREE_CODE (ttl) == FUNCTION_TYPE)
6466 {
6467 /* Because const and volatile on functions are
6468 restrictions that say the function will not do
6469 certain things, it is okay to use a const or volatile
6470 function where an ordinary one is wanted, but not
6471 vice-versa. */
6472 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6473 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6474 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6475 OPT_Wdiscarded_qualifiers,
6476 G_("passing argument %d of %qE "
6477 "makes %q#v qualified function "
6478 "pointer from unqualified"),
6479 G_("assignment makes %q#v qualified "
6480 "function pointer from "
6481 "unqualified"),
6482 G_("initialization makes %q#v qualified "
6483 "function pointer from "
6484 "unqualified"),
6485 G_("return makes %q#v qualified function "
6486 "pointer from unqualified"),
6487 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6488 }
6489 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6490 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6491 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6492 OPT_Wdiscarded_qualifiers,
6493 G_("passing argument %d of %qE discards "
6494 "%qv qualifier from pointer target type"),
6495 G_("assignment discards %qv qualifier "
6496 "from pointer target type"),
6497 G_("initialization discards %qv qualifier "
6498 "from pointer target type"),
6499 G_("return discards %qv qualifier from "
6500 "pointer target type"),
6501 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6502
6503 memb = marginal_memb;
6504 }
6505
6506 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6507 pedwarn (location, OPT_Wpedantic,
6508 "ISO C prohibits argument conversion to union type");
6509
6510 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6511 return build_constructor_single (type, memb, rhs);
6512 }
6513 }
6514
6515 /* Conversions among pointers */
6516 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6517 && (coder == codel))
6518 {
6519 tree ttl = TREE_TYPE (type);
6520 tree ttr = TREE_TYPE (rhstype);
6521 tree mvl = ttl;
6522 tree mvr = ttr;
6523 bool is_opaque_pointer;
6524 int target_cmp = 0; /* Cache comp_target_types () result. */
6525 addr_space_t asl;
6526 addr_space_t asr;
6527
6528 if (TREE_CODE (mvl) != ARRAY_TYPE)
6529 mvl = (TYPE_ATOMIC (mvl)
6530 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6531 TYPE_QUAL_ATOMIC)
6532 : TYPE_MAIN_VARIANT (mvl));
6533 if (TREE_CODE (mvr) != ARRAY_TYPE)
6534 mvr = (TYPE_ATOMIC (mvr)
6535 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6536 TYPE_QUAL_ATOMIC)
6537 : TYPE_MAIN_VARIANT (mvr));
6538 /* Opaque pointers are treated like void pointers. */
6539 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6540
6541 /* The Plan 9 compiler permits a pointer to a struct to be
6542 automatically converted into a pointer to an anonymous field
6543 within the struct. */
6544 if (flag_plan9_extensions
6545 && RECORD_OR_UNION_TYPE_P (mvl)
6546 && RECORD_OR_UNION_TYPE_P (mvr)
6547 && mvl != mvr)
6548 {
6549 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6550 if (new_rhs != NULL_TREE)
6551 {
6552 rhs = new_rhs;
6553 rhstype = TREE_TYPE (rhs);
6554 coder = TREE_CODE (rhstype);
6555 ttr = TREE_TYPE (rhstype);
6556 mvr = TYPE_MAIN_VARIANT (ttr);
6557 }
6558 }
6559
6560 /* C++ does not allow the implicit conversion void* -> T*. However,
6561 for the purpose of reducing the number of false positives, we
6562 tolerate the special case of
6563
6564 int *p = NULL;
6565
6566 where NULL is typically defined in C to be '(void *) 0'. */
6567 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6568 warning_at (errtype == ic_argpass ? expr_loc : location,
6569 OPT_Wc___compat,
6570 "request for implicit conversion "
6571 "from %qT to %qT not permitted in C++", rhstype, type);
6572
6573 /* See if the pointers point to incompatible address spaces. */
6574 asl = TYPE_ADDR_SPACE (ttl);
6575 asr = TYPE_ADDR_SPACE (ttr);
6576 if (!null_pointer_constant_p (rhs)
6577 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6578 {
6579 switch (errtype)
6580 {
6581 case ic_argpass:
6582 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6583 "non-enclosed address space", parmnum, rname);
6584 break;
6585 case ic_assign:
6586 error_at (location, "assignment from pointer to "
6587 "non-enclosed address space");
6588 break;
6589 case ic_init:
6590 error_at (location, "initialization from pointer to "
6591 "non-enclosed address space");
6592 break;
6593 case ic_return:
6594 error_at (location, "return from pointer to "
6595 "non-enclosed address space");
6596 break;
6597 default:
6598 gcc_unreachable ();
6599 }
6600 return error_mark_node;
6601 }
6602
6603 /* Check if the right-hand side has a format attribute but the
6604 left-hand side doesn't. */
6605 if (warn_suggest_attribute_format
6606 && check_missing_format_attribute (type, rhstype))
6607 {
6608 switch (errtype)
6609 {
6610 case ic_argpass:
6611 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6612 "argument %d of %qE might be "
6613 "a candidate for a format attribute",
6614 parmnum, rname);
6615 break;
6616 case ic_assign:
6617 warning_at (location, OPT_Wsuggest_attribute_format,
6618 "assignment left-hand side might be "
6619 "a candidate for a format attribute");
6620 break;
6621 case ic_init:
6622 warning_at (location, OPT_Wsuggest_attribute_format,
6623 "initialization left-hand side might be "
6624 "a candidate for a format attribute");
6625 break;
6626 case ic_return:
6627 warning_at (location, OPT_Wsuggest_attribute_format,
6628 "return type might be "
6629 "a candidate for a format attribute");
6630 break;
6631 default:
6632 gcc_unreachable ();
6633 }
6634 }
6635
6636 /* Any non-function converts to a [const][volatile] void *
6637 and vice versa; otherwise, targets must be the same.
6638 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6639 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6640 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6641 || (target_cmp = comp_target_types (location, type, rhstype))
6642 || is_opaque_pointer
6643 || ((c_common_unsigned_type (mvl)
6644 == c_common_unsigned_type (mvr))
6645 && (c_common_signed_type (mvl)
6646 == c_common_signed_type (mvr))
6647 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6648 {
6649 /* Warn about loss of qualifers from pointers to arrays with
6650 qualifiers on the element type. */
6651 if (TREE_CODE (ttr) == ARRAY_TYPE)
6652 {
6653 ttr = strip_array_types (ttr);
6654 ttl = strip_array_types (ttl);
6655
6656 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6657 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6658 WARNING_FOR_QUALIFIERS (location, expr_loc,
6659 OPT_Wdiscarded_array_qualifiers,
6660 G_("passing argument %d of %qE discards "
6661 "%qv qualifier from pointer target type"),
6662 G_("assignment discards %qv qualifier "
6663 "from pointer target type"),
6664 G_("initialization discards %qv qualifier "
6665 "from pointer target type"),
6666 G_("return discards %qv qualifier from "
6667 "pointer target type"),
6668 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6669 }
6670 else if (pedantic
6671 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6672 ||
6673 (VOID_TYPE_P (ttr)
6674 && !null_pointer_constant
6675 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6676 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6677 G_("ISO C forbids passing argument %d of "
6678 "%qE between function pointer "
6679 "and %<void *%>"),
6680 G_("ISO C forbids assignment between "
6681 "function pointer and %<void *%>"),
6682 G_("ISO C forbids initialization between "
6683 "function pointer and %<void *%>"),
6684 G_("ISO C forbids return between function "
6685 "pointer and %<void *%>"));
6686 /* Const and volatile mean something different for function types,
6687 so the usual warnings are not appropriate. */
6688 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6689 && TREE_CODE (ttl) != FUNCTION_TYPE)
6690 {
6691 /* Don't warn about loss of qualifier for conversions from
6692 qualified void* to pointers to arrays with corresponding
6693 qualifier on the element type. */
6694 if (!pedantic)
6695 ttl = strip_array_types (ttl);
6696
6697 /* Assignments between atomic and non-atomic objects are OK. */
6698 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6699 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6700 {
6701 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6702 OPT_Wdiscarded_qualifiers,
6703 G_("passing argument %d of %qE discards "
6704 "%qv qualifier from pointer target type"),
6705 G_("assignment discards %qv qualifier "
6706 "from pointer target type"),
6707 G_("initialization discards %qv qualifier "
6708 "from pointer target type"),
6709 G_("return discards %qv qualifier from "
6710 "pointer target type"),
6711 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6712 }
6713 /* If this is not a case of ignoring a mismatch in signedness,
6714 no warning. */
6715 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6716 || target_cmp)
6717 ;
6718 /* If there is a mismatch, do warn. */
6719 else if (warn_pointer_sign)
6720 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6721 G_("pointer targets in passing argument "
6722 "%d of %qE differ in signedness"),
6723 G_("pointer targets in assignment "
6724 "differ in signedness"),
6725 G_("pointer targets in initialization "
6726 "differ in signedness"),
6727 G_("pointer targets in return differ "
6728 "in signedness"));
6729 }
6730 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6731 && TREE_CODE (ttr) == FUNCTION_TYPE)
6732 {
6733 /* Because const and volatile on functions are restrictions
6734 that say the function will not do certain things,
6735 it is okay to use a const or volatile function
6736 where an ordinary one is wanted, but not vice-versa. */
6737 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6738 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6739 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6740 OPT_Wdiscarded_qualifiers,
6741 G_("passing argument %d of %qE makes "
6742 "%q#v qualified function pointer "
6743 "from unqualified"),
6744 G_("assignment makes %q#v qualified function "
6745 "pointer from unqualified"),
6746 G_("initialization makes %q#v qualified "
6747 "function pointer from unqualified"),
6748 G_("return makes %q#v qualified function "
6749 "pointer from unqualified"),
6750 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6751 }
6752 }
6753 else
6754 /* Avoid warning about the volatile ObjC EH puts on decls. */
6755 if (!objc_ok)
6756 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6757 OPT_Wincompatible_pointer_types,
6758 G_("passing argument %d of %qE from "
6759 "incompatible pointer type"),
6760 G_("assignment from incompatible pointer type"),
6761 G_("initialization from incompatible "
6762 "pointer type"),
6763 G_("return from incompatible pointer type"));
6764
6765 return convert (type, rhs);
6766 }
6767 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6768 {
6769 /* ??? This should not be an error when inlining calls to
6770 unprototyped functions. */
6771 error_at (location, "invalid use of non-lvalue array");
6772 return error_mark_node;
6773 }
6774 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6775 {
6776 /* An explicit constant 0 can convert to a pointer,
6777 or one that results from arithmetic, even including
6778 a cast to integer type. */
6779 if (!null_pointer_constant)
6780 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6781 OPT_Wint_conversion,
6782 G_("passing argument %d of %qE makes "
6783 "pointer from integer without a cast"),
6784 G_("assignment makes pointer from integer "
6785 "without a cast"),
6786 G_("initialization makes pointer from "
6787 "integer without a cast"),
6788 G_("return makes pointer from integer "
6789 "without a cast"));
6790
6791 return convert (type, rhs);
6792 }
6793 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6794 {
6795 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6796 OPT_Wint_conversion,
6797 G_("passing argument %d of %qE makes integer "
6798 "from pointer without a cast"),
6799 G_("assignment makes integer from pointer "
6800 "without a cast"),
6801 G_("initialization makes integer from pointer "
6802 "without a cast"),
6803 G_("return makes integer from pointer "
6804 "without a cast"));
6805 return convert (type, rhs);
6806 }
6807 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6808 {
6809 tree ret;
6810 bool save = in_late_binary_op;
6811 in_late_binary_op = true;
6812 ret = convert (type, rhs);
6813 in_late_binary_op = save;
6814 return ret;
6815 }
6816
6817 switch (errtype)
6818 {
6819 case ic_argpass:
6820 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6821 rname);
6822 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6823 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6824 "expected %qT but argument is of type %qT", type, rhstype);
6825 break;
6826 case ic_assign:
6827 error_at (location, "incompatible types when assigning to type %qT from "
6828 "type %qT", type, rhstype);
6829 break;
6830 case ic_init:
6831 error_at (location,
6832 "incompatible types when initializing type %qT using type %qT",
6833 type, rhstype);
6834 break;
6835 case ic_return:
6836 error_at (location,
6837 "incompatible types when returning type %qT but %qT was "
6838 "expected", rhstype, type);
6839 break;
6840 default:
6841 gcc_unreachable ();
6842 }
6843
6844 return error_mark_node;
6845 }
6846 \f
6847 /* If VALUE is a compound expr all of whose expressions are constant, then
6848 return its value. Otherwise, return error_mark_node.
6849
6850 This is for handling COMPOUND_EXPRs as initializer elements
6851 which is allowed with a warning when -pedantic is specified. */
6852
6853 static tree
6854 valid_compound_expr_initializer (tree value, tree endtype)
6855 {
6856 if (TREE_CODE (value) == COMPOUND_EXPR)
6857 {
6858 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6859 == error_mark_node)
6860 return error_mark_node;
6861 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6862 endtype);
6863 }
6864 else if (!initializer_constant_valid_p (value, endtype))
6865 return error_mark_node;
6866 else
6867 return value;
6868 }
6869 \f
6870 /* Perform appropriate conversions on the initial value of a variable,
6871 store it in the declaration DECL,
6872 and print any error messages that are appropriate.
6873 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6874 If the init is invalid, store an ERROR_MARK.
6875
6876 INIT_LOC is the location of the initial value. */
6877
6878 void
6879 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6880 {
6881 tree value, type;
6882 bool npc = false;
6883
6884 /* If variable's type was invalidly declared, just ignore it. */
6885
6886 type = TREE_TYPE (decl);
6887 if (TREE_CODE (type) == ERROR_MARK)
6888 return;
6889
6890 /* Digest the specified initializer into an expression. */
6891
6892 if (init)
6893 npc = null_pointer_constant_p (init);
6894 value = digest_init (init_loc, type, init, origtype, npc,
6895 true, TREE_STATIC (decl));
6896
6897 /* Store the expression if valid; else report error. */
6898
6899 if (!in_system_header_at (input_location)
6900 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6901 warning (OPT_Wtraditional, "traditional C rejects automatic "
6902 "aggregate initialization");
6903
6904 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6905 DECL_INITIAL (decl) = value;
6906
6907 /* ANSI wants warnings about out-of-range constant initializers. */
6908 STRIP_TYPE_NOPS (value);
6909 if (TREE_STATIC (decl))
6910 constant_expression_warning (value);
6911
6912 /* Check if we need to set array size from compound literal size. */
6913 if (TREE_CODE (type) == ARRAY_TYPE
6914 && TYPE_DOMAIN (type) == NULL_TREE
6915 && value != error_mark_node)
6916 {
6917 tree inside_init = init;
6918
6919 STRIP_TYPE_NOPS (inside_init);
6920 inside_init = fold (inside_init);
6921
6922 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6923 {
6924 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6925
6926 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6927 {
6928 /* For int foo[] = (int [3]){1}; we need to set array size
6929 now since later on array initializer will be just the
6930 brace enclosed list of the compound literal. */
6931 tree etype = strip_array_types (TREE_TYPE (decl));
6932 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6933 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6934 layout_type (type);
6935 layout_decl (cldecl, 0);
6936 TREE_TYPE (decl)
6937 = c_build_qualified_type (type, TYPE_QUALS (etype));
6938 }
6939 }
6940 }
6941 }
6942 \f
6943 /* Methods for storing and printing names for error messages. */
6944
6945 /* Implement a spelling stack that allows components of a name to be pushed
6946 and popped. Each element on the stack is this structure. */
6947
6948 struct spelling
6949 {
6950 int kind;
6951 union
6952 {
6953 unsigned HOST_WIDE_INT i;
6954 const char *s;
6955 } u;
6956 };
6957
6958 #define SPELLING_STRING 1
6959 #define SPELLING_MEMBER 2
6960 #define SPELLING_BOUNDS 3
6961
6962 static struct spelling *spelling; /* Next stack element (unused). */
6963 static struct spelling *spelling_base; /* Spelling stack base. */
6964 static int spelling_size; /* Size of the spelling stack. */
6965
6966 /* Macros to save and restore the spelling stack around push_... functions.
6967 Alternative to SAVE_SPELLING_STACK. */
6968
6969 #define SPELLING_DEPTH() (spelling - spelling_base)
6970 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6971
6972 /* Push an element on the spelling stack with type KIND and assign VALUE
6973 to MEMBER. */
6974
6975 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6976 { \
6977 int depth = SPELLING_DEPTH (); \
6978 \
6979 if (depth >= spelling_size) \
6980 { \
6981 spelling_size += 10; \
6982 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6983 spelling_size); \
6984 RESTORE_SPELLING_DEPTH (depth); \
6985 } \
6986 \
6987 spelling->kind = (KIND); \
6988 spelling->MEMBER = (VALUE); \
6989 spelling++; \
6990 }
6991
6992 /* Push STRING on the stack. Printed literally. */
6993
6994 static void
6995 push_string (const char *string)
6996 {
6997 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6998 }
6999
7000 /* Push a member name on the stack. Printed as '.' STRING. */
7001
7002 static void
7003 push_member_name (tree decl)
7004 {
7005 const char *const string
7006 = (DECL_NAME (decl)
7007 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7008 : _("<anonymous>"));
7009 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7010 }
7011
7012 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7013
7014 static void
7015 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7016 {
7017 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7018 }
7019
7020 /* Compute the maximum size in bytes of the printed spelling. */
7021
7022 static int
7023 spelling_length (void)
7024 {
7025 int size = 0;
7026 struct spelling *p;
7027
7028 for (p = spelling_base; p < spelling; p++)
7029 {
7030 if (p->kind == SPELLING_BOUNDS)
7031 size += 25;
7032 else
7033 size += strlen (p->u.s) + 1;
7034 }
7035
7036 return size;
7037 }
7038
7039 /* Print the spelling to BUFFER and return it. */
7040
7041 static char *
7042 print_spelling (char *buffer)
7043 {
7044 char *d = buffer;
7045 struct spelling *p;
7046
7047 for (p = spelling_base; p < spelling; p++)
7048 if (p->kind == SPELLING_BOUNDS)
7049 {
7050 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7051 d += strlen (d);
7052 }
7053 else
7054 {
7055 const char *s;
7056 if (p->kind == SPELLING_MEMBER)
7057 *d++ = '.';
7058 for (s = p->u.s; (*d = *s++); d++)
7059 ;
7060 }
7061 *d++ = '\0';
7062 return buffer;
7063 }
7064
7065 /* Digest the parser output INIT as an initializer for type TYPE.
7066 Return a C expression of type TYPE to represent the initial value.
7067
7068 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7069
7070 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7071
7072 If INIT is a string constant, STRICT_STRING is true if it is
7073 unparenthesized or we should not warn here for it being parenthesized.
7074 For other types of INIT, STRICT_STRING is not used.
7075
7076 INIT_LOC is the location of the INIT.
7077
7078 REQUIRE_CONSTANT requests an error if non-constant initializers or
7079 elements are seen. */
7080
7081 static tree
7082 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7083 bool null_pointer_constant, bool strict_string,
7084 int require_constant)
7085 {
7086 enum tree_code code = TREE_CODE (type);
7087 tree inside_init = init;
7088 tree semantic_type = NULL_TREE;
7089 bool maybe_const = true;
7090
7091 if (type == error_mark_node
7092 || !init
7093 || error_operand_p (init))
7094 return error_mark_node;
7095
7096 STRIP_TYPE_NOPS (inside_init);
7097
7098 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7099 {
7100 semantic_type = TREE_TYPE (inside_init);
7101 inside_init = TREE_OPERAND (inside_init, 0);
7102 }
7103 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7104 inside_init = decl_constant_value_for_optimization (inside_init);
7105
7106 /* Initialization of an array of chars from a string constant
7107 optionally enclosed in braces. */
7108
7109 if (code == ARRAY_TYPE && inside_init
7110 && TREE_CODE (inside_init) == STRING_CST)
7111 {
7112 tree typ1
7113 = (TYPE_ATOMIC (TREE_TYPE (type))
7114 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7115 TYPE_QUAL_ATOMIC)
7116 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7117 /* Note that an array could be both an array of character type
7118 and an array of wchar_t if wchar_t is signed char or unsigned
7119 char. */
7120 bool char_array = (typ1 == char_type_node
7121 || typ1 == signed_char_type_node
7122 || typ1 == unsigned_char_type_node);
7123 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7124 bool char16_array = !!comptypes (typ1, char16_type_node);
7125 bool char32_array = !!comptypes (typ1, char32_type_node);
7126
7127 if (char_array || wchar_array || char16_array || char32_array)
7128 {
7129 struct c_expr expr;
7130 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7131 expr.value = inside_init;
7132 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7133 expr.original_type = NULL;
7134 maybe_warn_string_init (init_loc, type, expr);
7135
7136 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7137 pedwarn_init (init_loc, OPT_Wpedantic,
7138 "initialization of a flexible array member");
7139
7140 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7141 TYPE_MAIN_VARIANT (type)))
7142 return inside_init;
7143
7144 if (char_array)
7145 {
7146 if (typ2 != char_type_node)
7147 {
7148 error_init (init_loc, "char-array initialized from wide "
7149 "string");
7150 return error_mark_node;
7151 }
7152 }
7153 else
7154 {
7155 if (typ2 == char_type_node)
7156 {
7157 error_init (init_loc, "wide character array initialized "
7158 "from non-wide string");
7159 return error_mark_node;
7160 }
7161 else if (!comptypes(typ1, typ2))
7162 {
7163 error_init (init_loc, "wide character array initialized "
7164 "from incompatible wide string");
7165 return error_mark_node;
7166 }
7167 }
7168
7169 TREE_TYPE (inside_init) = type;
7170 if (TYPE_DOMAIN (type) != NULL_TREE
7171 && TYPE_SIZE (type) != NULL_TREE
7172 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7173 {
7174 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7175
7176 /* Subtract the size of a single (possibly wide) character
7177 because it's ok to ignore the terminating null char
7178 that is counted in the length of the constant. */
7179 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7180 (len
7181 - (TYPE_PRECISION (typ1)
7182 / BITS_PER_UNIT))))
7183 pedwarn_init (init_loc, 0,
7184 ("initializer-string for array of chars "
7185 "is too long"));
7186 else if (warn_cxx_compat
7187 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7188 warning_at (init_loc, OPT_Wc___compat,
7189 ("initializer-string for array chars "
7190 "is too long for C++"));
7191 }
7192
7193 return inside_init;
7194 }
7195 else if (INTEGRAL_TYPE_P (typ1))
7196 {
7197 error_init (init_loc, "array of inappropriate type initialized "
7198 "from string constant");
7199 return error_mark_node;
7200 }
7201 }
7202
7203 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7204 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7205 below and handle as a constructor. */
7206 if (code == VECTOR_TYPE
7207 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7208 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7209 && TREE_CONSTANT (inside_init))
7210 {
7211 if (TREE_CODE (inside_init) == VECTOR_CST
7212 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7213 TYPE_MAIN_VARIANT (type)))
7214 return inside_init;
7215
7216 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7217 {
7218 unsigned HOST_WIDE_INT ix;
7219 tree value;
7220 bool constant_p = true;
7221
7222 /* Iterate through elements and check if all constructor
7223 elements are *_CSTs. */
7224 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7225 if (!CONSTANT_CLASS_P (value))
7226 {
7227 constant_p = false;
7228 break;
7229 }
7230
7231 if (constant_p)
7232 return build_vector_from_ctor (type,
7233 CONSTRUCTOR_ELTS (inside_init));
7234 }
7235 }
7236
7237 if (warn_sequence_point)
7238 verify_sequence_points (inside_init);
7239
7240 /* Any type can be initialized
7241 from an expression of the same type, optionally with braces. */
7242
7243 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7244 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7245 TYPE_MAIN_VARIANT (type))
7246 || (code == ARRAY_TYPE
7247 && comptypes (TREE_TYPE (inside_init), type))
7248 || (code == VECTOR_TYPE
7249 && comptypes (TREE_TYPE (inside_init), type))
7250 || (code == POINTER_TYPE
7251 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7252 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7253 TREE_TYPE (type)))))
7254 {
7255 if (code == POINTER_TYPE)
7256 {
7257 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7258 {
7259 if (TREE_CODE (inside_init) == STRING_CST
7260 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7261 inside_init = array_to_pointer_conversion
7262 (init_loc, inside_init);
7263 else
7264 {
7265 error_init (init_loc, "invalid use of non-lvalue array");
7266 return error_mark_node;
7267 }
7268 }
7269 }
7270
7271 if (code == VECTOR_TYPE)
7272 /* Although the types are compatible, we may require a
7273 conversion. */
7274 inside_init = convert (type, inside_init);
7275
7276 if (require_constant
7277 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7278 {
7279 /* As an extension, allow initializing objects with static storage
7280 duration with compound literals (which are then treated just as
7281 the brace enclosed list they contain). Also allow this for
7282 vectors, as we can only assign them with compound literals. */
7283 if (flag_isoc99 && code != VECTOR_TYPE)
7284 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7285 "is not constant");
7286 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7287 inside_init = DECL_INITIAL (decl);
7288 }
7289
7290 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7291 && TREE_CODE (inside_init) != CONSTRUCTOR)
7292 {
7293 error_init (init_loc, "array initialized from non-constant array "
7294 "expression");
7295 return error_mark_node;
7296 }
7297
7298 /* Compound expressions can only occur here if -Wpedantic or
7299 -pedantic-errors is specified. In the later case, we always want
7300 an error. In the former case, we simply want a warning. */
7301 if (require_constant && pedantic
7302 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7303 {
7304 inside_init
7305 = valid_compound_expr_initializer (inside_init,
7306 TREE_TYPE (inside_init));
7307 if (inside_init == error_mark_node)
7308 error_init (init_loc, "initializer element is not constant");
7309 else
7310 pedwarn_init (init_loc, OPT_Wpedantic,
7311 "initializer element is not constant");
7312 if (flag_pedantic_errors)
7313 inside_init = error_mark_node;
7314 }
7315 else if (require_constant
7316 && !initializer_constant_valid_p (inside_init,
7317 TREE_TYPE (inside_init)))
7318 {
7319 error_init (init_loc, "initializer element is not constant");
7320 inside_init = error_mark_node;
7321 }
7322 else if (require_constant && !maybe_const)
7323 pedwarn_init (init_loc, OPT_Wpedantic,
7324 "initializer element is not a constant expression");
7325
7326 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7327 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7328 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7329 type, inside_init, origtype,
7330 ic_init, null_pointer_constant,
7331 NULL_TREE, NULL_TREE, 0);
7332 return inside_init;
7333 }
7334
7335 /* Handle scalar types, including conversions. */
7336
7337 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7338 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7339 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7340 {
7341 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7342 && (TREE_CODE (init) == STRING_CST
7343 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7344 inside_init = init = array_to_pointer_conversion (init_loc, init);
7345 if (semantic_type)
7346 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7347 inside_init);
7348 inside_init
7349 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7350 inside_init, origtype, ic_init,
7351 null_pointer_constant, NULL_TREE, NULL_TREE,
7352 0);
7353
7354 /* Check to see if we have already given an error message. */
7355 if (inside_init == error_mark_node)
7356 ;
7357 else if (require_constant && !TREE_CONSTANT (inside_init))
7358 {
7359 error_init (init_loc, "initializer element is not constant");
7360 inside_init = error_mark_node;
7361 }
7362 else if (require_constant
7363 && !initializer_constant_valid_p (inside_init,
7364 TREE_TYPE (inside_init)))
7365 {
7366 error_init (init_loc, "initializer element is not computable at "
7367 "load time");
7368 inside_init = error_mark_node;
7369 }
7370 else if (require_constant && !maybe_const)
7371 pedwarn_init (init_loc, OPT_Wpedantic,
7372 "initializer element is not a constant expression");
7373
7374 return inside_init;
7375 }
7376
7377 /* Come here only for records and arrays. */
7378
7379 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7380 {
7381 error_init (init_loc, "variable-sized object may not be initialized");
7382 return error_mark_node;
7383 }
7384
7385 error_init (init_loc, "invalid initializer");
7386 return error_mark_node;
7387 }
7388 \f
7389 /* Handle initializers that use braces. */
7390
7391 /* Type of object we are accumulating a constructor for.
7392 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7393 static tree constructor_type;
7394
7395 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7396 left to fill. */
7397 static tree constructor_fields;
7398
7399 /* For an ARRAY_TYPE, this is the specified index
7400 at which to store the next element we get. */
7401 static tree constructor_index;
7402
7403 /* For an ARRAY_TYPE, this is the maximum index. */
7404 static tree constructor_max_index;
7405
7406 /* For a RECORD_TYPE, this is the first field not yet written out. */
7407 static tree constructor_unfilled_fields;
7408
7409 /* For an ARRAY_TYPE, this is the index of the first element
7410 not yet written out. */
7411 static tree constructor_unfilled_index;
7412
7413 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7414 This is so we can generate gaps between fields, when appropriate. */
7415 static tree constructor_bit_index;
7416
7417 /* If we are saving up the elements rather than allocating them,
7418 this is the list of elements so far (in reverse order,
7419 most recent first). */
7420 static vec<constructor_elt, va_gc> *constructor_elements;
7421
7422 /* 1 if constructor should be incrementally stored into a constructor chain,
7423 0 if all the elements should be kept in AVL tree. */
7424 static int constructor_incremental;
7425
7426 /* 1 if so far this constructor's elements are all compile-time constants. */
7427 static int constructor_constant;
7428
7429 /* 1 if so far this constructor's elements are all valid address constants. */
7430 static int constructor_simple;
7431
7432 /* 1 if this constructor has an element that cannot be part of a
7433 constant expression. */
7434 static int constructor_nonconst;
7435
7436 /* 1 if this constructor is erroneous so far. */
7437 static int constructor_erroneous;
7438
7439 /* 1 if this constructor is the universal zero initializer { 0 }. */
7440 static int constructor_zeroinit;
7441
7442 /* Structure for managing pending initializer elements, organized as an
7443 AVL tree. */
7444
7445 struct init_node
7446 {
7447 struct init_node *left, *right;
7448 struct init_node *parent;
7449 int balance;
7450 tree purpose;
7451 tree value;
7452 tree origtype;
7453 };
7454
7455 /* Tree of pending elements at this constructor level.
7456 These are elements encountered out of order
7457 which belong at places we haven't reached yet in actually
7458 writing the output.
7459 Will never hold tree nodes across GC runs. */
7460 static struct init_node *constructor_pending_elts;
7461
7462 /* The SPELLING_DEPTH of this constructor. */
7463 static int constructor_depth;
7464
7465 /* DECL node for which an initializer is being read.
7466 0 means we are reading a constructor expression
7467 such as (struct foo) {...}. */
7468 static tree constructor_decl;
7469
7470 /* Nonzero if this is an initializer for a top-level decl. */
7471 static int constructor_top_level;
7472
7473 /* Nonzero if there were any member designators in this initializer. */
7474 static int constructor_designated;
7475
7476 /* Nesting depth of designator list. */
7477 static int designator_depth;
7478
7479 /* Nonzero if there were diagnosed errors in this designator list. */
7480 static int designator_erroneous;
7481
7482 \f
7483 /* This stack has a level for each implicit or explicit level of
7484 structuring in the initializer, including the outermost one. It
7485 saves the values of most of the variables above. */
7486
7487 struct constructor_range_stack;
7488
7489 struct constructor_stack
7490 {
7491 struct constructor_stack *next;
7492 tree type;
7493 tree fields;
7494 tree index;
7495 tree max_index;
7496 tree unfilled_index;
7497 tree unfilled_fields;
7498 tree bit_index;
7499 vec<constructor_elt, va_gc> *elements;
7500 struct init_node *pending_elts;
7501 int offset;
7502 int depth;
7503 /* If value nonzero, this value should replace the entire
7504 constructor at this level. */
7505 struct c_expr replacement_value;
7506 struct constructor_range_stack *range_stack;
7507 char constant;
7508 char simple;
7509 char nonconst;
7510 char implicit;
7511 char erroneous;
7512 char outer;
7513 char incremental;
7514 char designated;
7515 int designator_depth;
7516 };
7517
7518 static struct constructor_stack *constructor_stack;
7519
7520 /* This stack represents designators from some range designator up to
7521 the last designator in the list. */
7522
7523 struct constructor_range_stack
7524 {
7525 struct constructor_range_stack *next, *prev;
7526 struct constructor_stack *stack;
7527 tree range_start;
7528 tree index;
7529 tree range_end;
7530 tree fields;
7531 };
7532
7533 static struct constructor_range_stack *constructor_range_stack;
7534
7535 /* This stack records separate initializers that are nested.
7536 Nested initializers can't happen in ANSI C, but GNU C allows them
7537 in cases like { ... (struct foo) { ... } ... }. */
7538
7539 struct initializer_stack
7540 {
7541 struct initializer_stack *next;
7542 tree decl;
7543 struct constructor_stack *constructor_stack;
7544 struct constructor_range_stack *constructor_range_stack;
7545 vec<constructor_elt, va_gc> *elements;
7546 struct spelling *spelling;
7547 struct spelling *spelling_base;
7548 int spelling_size;
7549 char top_level;
7550 char require_constant_value;
7551 char require_constant_elements;
7552 rich_location *missing_brace_richloc;
7553 };
7554
7555 static struct initializer_stack *initializer_stack;
7556 \f
7557 /* Prepare to parse and output the initializer for variable DECL. */
7558
7559 void
7560 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
7561 rich_location *richloc)
7562 {
7563 const char *locus;
7564 struct initializer_stack *p = XNEW (struct initializer_stack);
7565
7566 p->decl = constructor_decl;
7567 p->require_constant_value = require_constant_value;
7568 p->require_constant_elements = require_constant_elements;
7569 p->constructor_stack = constructor_stack;
7570 p->constructor_range_stack = constructor_range_stack;
7571 p->elements = constructor_elements;
7572 p->spelling = spelling;
7573 p->spelling_base = spelling_base;
7574 p->spelling_size = spelling_size;
7575 p->top_level = constructor_top_level;
7576 p->next = initializer_stack;
7577 p->missing_brace_richloc = richloc;
7578 initializer_stack = p;
7579
7580 constructor_decl = decl;
7581 constructor_designated = 0;
7582 constructor_top_level = top_level;
7583
7584 if (decl != NULL_TREE && decl != error_mark_node)
7585 {
7586 require_constant_value = TREE_STATIC (decl);
7587 require_constant_elements
7588 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7589 /* For a scalar, you can always use any value to initialize,
7590 even within braces. */
7591 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7592 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7593 }
7594 else
7595 {
7596 require_constant_value = 0;
7597 require_constant_elements = 0;
7598 locus = _("(anonymous)");
7599 }
7600
7601 constructor_stack = 0;
7602 constructor_range_stack = 0;
7603
7604 found_missing_braces = 0;
7605
7606 spelling_base = 0;
7607 spelling_size = 0;
7608 RESTORE_SPELLING_DEPTH (0);
7609
7610 if (locus)
7611 push_string (locus);
7612 }
7613
7614 void
7615 finish_init (void)
7616 {
7617 struct initializer_stack *p = initializer_stack;
7618
7619 /* Free the whole constructor stack of this initializer. */
7620 while (constructor_stack)
7621 {
7622 struct constructor_stack *q = constructor_stack;
7623 constructor_stack = q->next;
7624 free (q);
7625 }
7626
7627 gcc_assert (!constructor_range_stack);
7628
7629 /* Pop back to the data of the outer initializer (if any). */
7630 free (spelling_base);
7631
7632 constructor_decl = p->decl;
7633 require_constant_value = p->require_constant_value;
7634 require_constant_elements = p->require_constant_elements;
7635 constructor_stack = p->constructor_stack;
7636 constructor_range_stack = p->constructor_range_stack;
7637 constructor_elements = p->elements;
7638 spelling = p->spelling;
7639 spelling_base = p->spelling_base;
7640 spelling_size = p->spelling_size;
7641 constructor_top_level = p->top_level;
7642 initializer_stack = p->next;
7643 free (p);
7644 }
7645 \f
7646 /* Call here when we see the initializer is surrounded by braces.
7647 This is instead of a call to push_init_level;
7648 it is matched by a call to pop_init_level.
7649
7650 TYPE is the type to initialize, for a constructor expression.
7651 For an initializer for a decl, TYPE is zero. */
7652
7653 void
7654 really_start_incremental_init (tree type)
7655 {
7656 struct constructor_stack *p = XNEW (struct constructor_stack);
7657
7658 if (type == NULL_TREE)
7659 type = TREE_TYPE (constructor_decl);
7660
7661 if (VECTOR_TYPE_P (type)
7662 && TYPE_VECTOR_OPAQUE (type))
7663 error ("opaque vector types cannot be initialized");
7664
7665 p->type = constructor_type;
7666 p->fields = constructor_fields;
7667 p->index = constructor_index;
7668 p->max_index = constructor_max_index;
7669 p->unfilled_index = constructor_unfilled_index;
7670 p->unfilled_fields = constructor_unfilled_fields;
7671 p->bit_index = constructor_bit_index;
7672 p->elements = constructor_elements;
7673 p->constant = constructor_constant;
7674 p->simple = constructor_simple;
7675 p->nonconst = constructor_nonconst;
7676 p->erroneous = constructor_erroneous;
7677 p->pending_elts = constructor_pending_elts;
7678 p->depth = constructor_depth;
7679 p->replacement_value.value = 0;
7680 p->replacement_value.original_code = ERROR_MARK;
7681 p->replacement_value.original_type = NULL;
7682 p->implicit = 0;
7683 p->range_stack = 0;
7684 p->outer = 0;
7685 p->incremental = constructor_incremental;
7686 p->designated = constructor_designated;
7687 p->designator_depth = designator_depth;
7688 p->next = 0;
7689 constructor_stack = p;
7690
7691 constructor_constant = 1;
7692 constructor_simple = 1;
7693 constructor_nonconst = 0;
7694 constructor_depth = SPELLING_DEPTH ();
7695 constructor_elements = NULL;
7696 constructor_pending_elts = 0;
7697 constructor_type = type;
7698 constructor_incremental = 1;
7699 constructor_designated = 0;
7700 constructor_zeroinit = 1;
7701 designator_depth = 0;
7702 designator_erroneous = 0;
7703
7704 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7705 {
7706 constructor_fields = TYPE_FIELDS (constructor_type);
7707 /* Skip any nameless bit fields at the beginning. */
7708 while (constructor_fields != NULL_TREE
7709 && DECL_C_BIT_FIELD (constructor_fields)
7710 && DECL_NAME (constructor_fields) == NULL_TREE)
7711 constructor_fields = DECL_CHAIN (constructor_fields);
7712
7713 constructor_unfilled_fields = constructor_fields;
7714 constructor_bit_index = bitsize_zero_node;
7715 }
7716 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7717 {
7718 if (TYPE_DOMAIN (constructor_type))
7719 {
7720 constructor_max_index
7721 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7722
7723 /* Detect non-empty initializations of zero-length arrays. */
7724 if (constructor_max_index == NULL_TREE
7725 && TYPE_SIZE (constructor_type))
7726 constructor_max_index = integer_minus_one_node;
7727
7728 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7729 to initialize VLAs will cause a proper error; avoid tree
7730 checking errors as well by setting a safe value. */
7731 if (constructor_max_index
7732 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7733 constructor_max_index = integer_minus_one_node;
7734
7735 constructor_index
7736 = convert (bitsizetype,
7737 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7738 }
7739 else
7740 {
7741 constructor_index = bitsize_zero_node;
7742 constructor_max_index = NULL_TREE;
7743 }
7744
7745 constructor_unfilled_index = constructor_index;
7746 }
7747 else if (VECTOR_TYPE_P (constructor_type))
7748 {
7749 /* Vectors are like simple fixed-size arrays. */
7750 constructor_max_index =
7751 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7752 constructor_index = bitsize_zero_node;
7753 constructor_unfilled_index = constructor_index;
7754 }
7755 else
7756 {
7757 /* Handle the case of int x = {5}; */
7758 constructor_fields = constructor_type;
7759 constructor_unfilled_fields = constructor_type;
7760 }
7761 }
7762 \f
7763 extern location_t last_init_list_comma;
7764
7765 /* Called when we see an open brace for a nested initializer. Finish
7766 off any pending levels with implicit braces. */
7767 void
7768 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7769 {
7770 while (constructor_stack->implicit)
7771 {
7772 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7773 && constructor_fields == NULL_TREE)
7774 process_init_element (input_location,
7775 pop_init_level (loc, 1, braced_init_obstack,
7776 last_init_list_comma),
7777 true, braced_init_obstack);
7778 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7779 && constructor_max_index
7780 && tree_int_cst_lt (constructor_max_index,
7781 constructor_index))
7782 process_init_element (input_location,
7783 pop_init_level (loc, 1, braced_init_obstack,
7784 last_init_list_comma),
7785 true, braced_init_obstack);
7786 else
7787 break;
7788 }
7789 }
7790
7791 /* Push down into a subobject, for initialization.
7792 If this is for an explicit set of braces, IMPLICIT is 0.
7793 If it is because the next element belongs at a lower level,
7794 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7795
7796 void
7797 push_init_level (location_t loc, int implicit,
7798 struct obstack *braced_init_obstack)
7799 {
7800 struct constructor_stack *p;
7801 tree value = NULL_TREE;
7802
7803 /* Unless this is an explicit brace, we need to preserve previous
7804 content if any. */
7805 if (implicit)
7806 {
7807 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
7808 value = find_init_member (constructor_fields, braced_init_obstack);
7809 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7810 value = find_init_member (constructor_index, braced_init_obstack);
7811 }
7812
7813 p = XNEW (struct constructor_stack);
7814 p->type = constructor_type;
7815 p->fields = constructor_fields;
7816 p->index = constructor_index;
7817 p->max_index = constructor_max_index;
7818 p->unfilled_index = constructor_unfilled_index;
7819 p->unfilled_fields = constructor_unfilled_fields;
7820 p->bit_index = constructor_bit_index;
7821 p->elements = constructor_elements;
7822 p->constant = constructor_constant;
7823 p->simple = constructor_simple;
7824 p->nonconst = constructor_nonconst;
7825 p->erroneous = constructor_erroneous;
7826 p->pending_elts = constructor_pending_elts;
7827 p->depth = constructor_depth;
7828 p->replacement_value.value = NULL_TREE;
7829 p->replacement_value.original_code = ERROR_MARK;
7830 p->replacement_value.original_type = NULL;
7831 p->implicit = implicit;
7832 p->outer = 0;
7833 p->incremental = constructor_incremental;
7834 p->designated = constructor_designated;
7835 p->designator_depth = designator_depth;
7836 p->next = constructor_stack;
7837 p->range_stack = 0;
7838 constructor_stack = p;
7839
7840 constructor_constant = 1;
7841 constructor_simple = 1;
7842 constructor_nonconst = 0;
7843 constructor_depth = SPELLING_DEPTH ();
7844 constructor_elements = NULL;
7845 constructor_incremental = 1;
7846 constructor_designated = 0;
7847 constructor_pending_elts = 0;
7848 if (!implicit)
7849 {
7850 p->range_stack = constructor_range_stack;
7851 constructor_range_stack = 0;
7852 designator_depth = 0;
7853 designator_erroneous = 0;
7854 }
7855
7856 /* Don't die if an entire brace-pair level is superfluous
7857 in the containing level. */
7858 if (constructor_type == NULL_TREE)
7859 ;
7860 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
7861 {
7862 /* Don't die if there are extra init elts at the end. */
7863 if (constructor_fields == NULL_TREE)
7864 constructor_type = NULL_TREE;
7865 else
7866 {
7867 constructor_type = TREE_TYPE (constructor_fields);
7868 push_member_name (constructor_fields);
7869 constructor_depth++;
7870 }
7871 /* If upper initializer is designated, then mark this as
7872 designated too to prevent bogus warnings. */
7873 constructor_designated = p->designated;
7874 }
7875 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7876 {
7877 constructor_type = TREE_TYPE (constructor_type);
7878 push_array_bounds (tree_to_uhwi (constructor_index));
7879 constructor_depth++;
7880 }
7881
7882 if (constructor_type == NULL_TREE)
7883 {
7884 error_init (loc, "extra brace group at end of initializer");
7885 constructor_fields = NULL_TREE;
7886 constructor_unfilled_fields = NULL_TREE;
7887 return;
7888 }
7889
7890 if (value && TREE_CODE (value) == CONSTRUCTOR)
7891 {
7892 constructor_constant = TREE_CONSTANT (value);
7893 constructor_simple = TREE_STATIC (value);
7894 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7895 constructor_elements = CONSTRUCTOR_ELTS (value);
7896 if (!vec_safe_is_empty (constructor_elements)
7897 && (TREE_CODE (constructor_type) == RECORD_TYPE
7898 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7899 set_nonincremental_init (braced_init_obstack);
7900 }
7901
7902 if (implicit == 1)
7903 {
7904 found_missing_braces = 1;
7905 if (initializer_stack->missing_brace_richloc)
7906 initializer_stack->missing_brace_richloc->add_fixit_insert_before
7907 (loc, "{");
7908 }
7909
7910 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7911 {
7912 constructor_fields = TYPE_FIELDS (constructor_type);
7913 /* Skip any nameless bit fields at the beginning. */
7914 while (constructor_fields != NULL_TREE
7915 && DECL_C_BIT_FIELD (constructor_fields)
7916 && DECL_NAME (constructor_fields) == NULL_TREE)
7917 constructor_fields = DECL_CHAIN (constructor_fields);
7918
7919 constructor_unfilled_fields = constructor_fields;
7920 constructor_bit_index = bitsize_zero_node;
7921 }
7922 else if (VECTOR_TYPE_P (constructor_type))
7923 {
7924 /* Vectors are like simple fixed-size arrays. */
7925 constructor_max_index =
7926 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7927 constructor_index = bitsize_int (0);
7928 constructor_unfilled_index = constructor_index;
7929 }
7930 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7931 {
7932 if (TYPE_DOMAIN (constructor_type))
7933 {
7934 constructor_max_index
7935 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7936
7937 /* Detect non-empty initializations of zero-length arrays. */
7938 if (constructor_max_index == NULL_TREE
7939 && TYPE_SIZE (constructor_type))
7940 constructor_max_index = integer_minus_one_node;
7941
7942 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7943 to initialize VLAs will cause a proper error; avoid tree
7944 checking errors as well by setting a safe value. */
7945 if (constructor_max_index
7946 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7947 constructor_max_index = integer_minus_one_node;
7948
7949 constructor_index
7950 = convert (bitsizetype,
7951 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7952 }
7953 else
7954 constructor_index = bitsize_zero_node;
7955
7956 constructor_unfilled_index = constructor_index;
7957 if (value && TREE_CODE (value) == STRING_CST)
7958 {
7959 /* We need to split the char/wchar array into individual
7960 characters, so that we don't have to special case it
7961 everywhere. */
7962 set_nonincremental_init_from_string (value, braced_init_obstack);
7963 }
7964 }
7965 else
7966 {
7967 if (constructor_type != error_mark_node)
7968 warning_init (input_location, 0, "braces around scalar initializer");
7969 constructor_fields = constructor_type;
7970 constructor_unfilled_fields = constructor_type;
7971 }
7972 }
7973
7974 /* At the end of an implicit or explicit brace level,
7975 finish up that level of constructor. If a single expression
7976 with redundant braces initialized that level, return the
7977 c_expr structure for that expression. Otherwise, the original_code
7978 element is set to ERROR_MARK.
7979 If we were outputting the elements as they are read, return 0 as the value
7980 from inner levels (process_init_element ignores that),
7981 but return error_mark_node as the value from the outermost level
7982 (that's what we want to put in DECL_INITIAL).
7983 Otherwise, return a CONSTRUCTOR expression as the value. */
7984
7985 struct c_expr
7986 pop_init_level (location_t loc, int implicit,
7987 struct obstack *braced_init_obstack,
7988 location_t insert_before)
7989 {
7990 struct constructor_stack *p;
7991 struct c_expr ret;
7992 ret.value = NULL_TREE;
7993 ret.original_code = ERROR_MARK;
7994 ret.original_type = NULL;
7995
7996 if (implicit == 0)
7997 {
7998 /* When we come to an explicit close brace,
7999 pop any inner levels that didn't have explicit braces. */
8000 while (constructor_stack->implicit)
8001 process_init_element (input_location,
8002 pop_init_level (loc, 1, braced_init_obstack,
8003 insert_before),
8004 true, braced_init_obstack);
8005 gcc_assert (!constructor_range_stack);
8006 }
8007 else
8008 if (initializer_stack->missing_brace_richloc)
8009 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8010 (insert_before, "}");
8011
8012 /* Now output all pending elements. */
8013 constructor_incremental = 1;
8014 output_pending_init_elements (1, braced_init_obstack);
8015
8016 p = constructor_stack;
8017
8018 /* Error for initializing a flexible array member, or a zero-length
8019 array member in an inappropriate context. */
8020 if (constructor_type && constructor_fields
8021 && TREE_CODE (constructor_type) == ARRAY_TYPE
8022 && TYPE_DOMAIN (constructor_type)
8023 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8024 {
8025 /* Silently discard empty initializations. The parser will
8026 already have pedwarned for empty brackets. */
8027 if (integer_zerop (constructor_unfilled_index))
8028 constructor_type = NULL_TREE;
8029 else
8030 {
8031 gcc_assert (!TYPE_SIZE (constructor_type));
8032
8033 if (constructor_depth > 2)
8034 error_init (loc, "initialization of flexible array member in a nested context");
8035 else
8036 pedwarn_init (loc, OPT_Wpedantic,
8037 "initialization of a flexible array member");
8038
8039 /* We have already issued an error message for the existence
8040 of a flexible array member not at the end of the structure.
8041 Discard the initializer so that we do not die later. */
8042 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8043 constructor_type = NULL_TREE;
8044 }
8045 }
8046
8047 switch (vec_safe_length (constructor_elements))
8048 {
8049 case 0:
8050 /* Initialization with { } counts as zeroinit. */
8051 constructor_zeroinit = 1;
8052 break;
8053 case 1:
8054 /* This might be zeroinit as well. */
8055 if (integer_zerop ((*constructor_elements)[0].value))
8056 constructor_zeroinit = 1;
8057 break;
8058 default:
8059 /* If the constructor has more than one element, it can't be { 0 }. */
8060 constructor_zeroinit = 0;
8061 break;
8062 }
8063
8064 /* Warn when some structs are initialized with direct aggregation. */
8065 if (!implicit && found_missing_braces && warn_missing_braces
8066 && !constructor_zeroinit)
8067 {
8068 gcc_assert (initializer_stack->missing_brace_richloc);
8069 warning_at_rich_loc (initializer_stack->missing_brace_richloc,
8070 OPT_Wmissing_braces,
8071 "missing braces around initializer");
8072 }
8073
8074 /* Warn when some struct elements are implicitly initialized to zero. */
8075 if (warn_missing_field_initializers
8076 && constructor_type
8077 && TREE_CODE (constructor_type) == RECORD_TYPE
8078 && constructor_unfilled_fields)
8079 {
8080 /* Do not warn for flexible array members or zero-length arrays. */
8081 while (constructor_unfilled_fields
8082 && (!DECL_SIZE (constructor_unfilled_fields)
8083 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8084 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8085
8086 if (constructor_unfilled_fields
8087 /* Do not warn if this level of the initializer uses member
8088 designators; it is likely to be deliberate. */
8089 && !constructor_designated
8090 /* Do not warn about initializing with { 0 } or with { }. */
8091 && !constructor_zeroinit)
8092 {
8093 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8094 "missing initializer for field %qD of %qT",
8095 constructor_unfilled_fields,
8096 constructor_type))
8097 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8098 "%qD declared here", constructor_unfilled_fields);
8099 }
8100 }
8101
8102 /* Pad out the end of the structure. */
8103 if (p->replacement_value.value)
8104 /* If this closes a superfluous brace pair,
8105 just pass out the element between them. */
8106 ret = p->replacement_value;
8107 else if (constructor_type == NULL_TREE)
8108 ;
8109 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8110 && TREE_CODE (constructor_type) != ARRAY_TYPE
8111 && !VECTOR_TYPE_P (constructor_type))
8112 {
8113 /* A nonincremental scalar initializer--just return
8114 the element, after verifying there is just one. */
8115 if (vec_safe_is_empty (constructor_elements))
8116 {
8117 if (!constructor_erroneous)
8118 error_init (loc, "empty scalar initializer");
8119 ret.value = error_mark_node;
8120 }
8121 else if (vec_safe_length (constructor_elements) != 1)
8122 {
8123 error_init (loc, "extra elements in scalar initializer");
8124 ret.value = (*constructor_elements)[0].value;
8125 }
8126 else
8127 ret.value = (*constructor_elements)[0].value;
8128 }
8129 else
8130 {
8131 if (constructor_erroneous)
8132 ret.value = error_mark_node;
8133 else
8134 {
8135 ret.value = build_constructor (constructor_type,
8136 constructor_elements);
8137 if (constructor_constant)
8138 TREE_CONSTANT (ret.value) = 1;
8139 if (constructor_constant && constructor_simple)
8140 TREE_STATIC (ret.value) = 1;
8141 if (constructor_nonconst)
8142 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8143 }
8144 }
8145
8146 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8147 {
8148 if (constructor_nonconst)
8149 ret.original_code = C_MAYBE_CONST_EXPR;
8150 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8151 ret.original_code = ERROR_MARK;
8152 }
8153
8154 constructor_type = p->type;
8155 constructor_fields = p->fields;
8156 constructor_index = p->index;
8157 constructor_max_index = p->max_index;
8158 constructor_unfilled_index = p->unfilled_index;
8159 constructor_unfilled_fields = p->unfilled_fields;
8160 constructor_bit_index = p->bit_index;
8161 constructor_elements = p->elements;
8162 constructor_constant = p->constant;
8163 constructor_simple = p->simple;
8164 constructor_nonconst = p->nonconst;
8165 constructor_erroneous = p->erroneous;
8166 constructor_incremental = p->incremental;
8167 constructor_designated = p->designated;
8168 designator_depth = p->designator_depth;
8169 constructor_pending_elts = p->pending_elts;
8170 constructor_depth = p->depth;
8171 if (!p->implicit)
8172 constructor_range_stack = p->range_stack;
8173 RESTORE_SPELLING_DEPTH (constructor_depth);
8174
8175 constructor_stack = p->next;
8176 free (p);
8177
8178 if (ret.value == NULL_TREE && constructor_stack == 0)
8179 ret.value = error_mark_node;
8180 return ret;
8181 }
8182
8183 /* Common handling for both array range and field name designators.
8184 ARRAY argument is nonzero for array ranges. Returns false for success. */
8185
8186 static bool
8187 set_designator (location_t loc, int array,
8188 struct obstack *braced_init_obstack)
8189 {
8190 tree subtype;
8191 enum tree_code subcode;
8192
8193 /* Don't die if an entire brace-pair level is superfluous
8194 in the containing level. */
8195 if (constructor_type == NULL_TREE)
8196 return true;
8197
8198 /* If there were errors in this designator list already, bail out
8199 silently. */
8200 if (designator_erroneous)
8201 return true;
8202
8203 if (!designator_depth)
8204 {
8205 gcc_assert (!constructor_range_stack);
8206
8207 /* Designator list starts at the level of closest explicit
8208 braces. */
8209 while (constructor_stack->implicit)
8210 process_init_element (input_location,
8211 pop_init_level (loc, 1, braced_init_obstack,
8212 last_init_list_comma),
8213 true, braced_init_obstack);
8214 constructor_designated = 1;
8215 return false;
8216 }
8217
8218 switch (TREE_CODE (constructor_type))
8219 {
8220 case RECORD_TYPE:
8221 case UNION_TYPE:
8222 subtype = TREE_TYPE (constructor_fields);
8223 if (subtype != error_mark_node)
8224 subtype = TYPE_MAIN_VARIANT (subtype);
8225 break;
8226 case ARRAY_TYPE:
8227 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8228 break;
8229 default:
8230 gcc_unreachable ();
8231 }
8232
8233 subcode = TREE_CODE (subtype);
8234 if (array && subcode != ARRAY_TYPE)
8235 {
8236 error_init (loc, "array index in non-array initializer");
8237 return true;
8238 }
8239 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8240 {
8241 error_init (loc, "field name not in record or union initializer");
8242 return true;
8243 }
8244
8245 constructor_designated = 1;
8246 finish_implicit_inits (loc, braced_init_obstack);
8247 push_init_level (loc, 2, braced_init_obstack);
8248 return false;
8249 }
8250
8251 /* If there are range designators in designator list, push a new designator
8252 to constructor_range_stack. RANGE_END is end of such stack range or
8253 NULL_TREE if there is no range designator at this level. */
8254
8255 static void
8256 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8257 {
8258 struct constructor_range_stack *p;
8259
8260 p = (struct constructor_range_stack *)
8261 obstack_alloc (braced_init_obstack,
8262 sizeof (struct constructor_range_stack));
8263 p->prev = constructor_range_stack;
8264 p->next = 0;
8265 p->fields = constructor_fields;
8266 p->range_start = constructor_index;
8267 p->index = constructor_index;
8268 p->stack = constructor_stack;
8269 p->range_end = range_end;
8270 if (constructor_range_stack)
8271 constructor_range_stack->next = p;
8272 constructor_range_stack = p;
8273 }
8274
8275 /* Within an array initializer, specify the next index to be initialized.
8276 FIRST is that index. If LAST is nonzero, then initialize a range
8277 of indices, running from FIRST through LAST. */
8278
8279 void
8280 set_init_index (location_t loc, tree first, tree last,
8281 struct obstack *braced_init_obstack)
8282 {
8283 if (set_designator (loc, 1, braced_init_obstack))
8284 return;
8285
8286 designator_erroneous = 1;
8287
8288 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8289 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8290 {
8291 error_init (loc, "array index in initializer not of integer type");
8292 return;
8293 }
8294
8295 if (TREE_CODE (first) != INTEGER_CST)
8296 {
8297 first = c_fully_fold (first, false, NULL);
8298 if (TREE_CODE (first) == INTEGER_CST)
8299 pedwarn_init (loc, OPT_Wpedantic,
8300 "array index in initializer is not "
8301 "an integer constant expression");
8302 }
8303
8304 if (last && TREE_CODE (last) != INTEGER_CST)
8305 {
8306 last = c_fully_fold (last, false, NULL);
8307 if (TREE_CODE (last) == INTEGER_CST)
8308 pedwarn_init (loc, OPT_Wpedantic,
8309 "array index in initializer is not "
8310 "an integer constant expression");
8311 }
8312
8313 if (TREE_CODE (first) != INTEGER_CST)
8314 error_init (loc, "nonconstant array index in initializer");
8315 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
8316 error_init (loc, "nonconstant array index in initializer");
8317 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8318 error_init (loc, "array index in non-array initializer");
8319 else if (tree_int_cst_sgn (first) == -1)
8320 error_init (loc, "array index in initializer exceeds array bounds");
8321 else if (constructor_max_index
8322 && tree_int_cst_lt (constructor_max_index, first))
8323 error_init (loc, "array index in initializer exceeds array bounds");
8324 else
8325 {
8326 constant_expression_warning (first);
8327 if (last)
8328 constant_expression_warning (last);
8329 constructor_index = convert (bitsizetype, first);
8330 if (tree_int_cst_lt (constructor_index, first))
8331 {
8332 constructor_index = copy_node (constructor_index);
8333 TREE_OVERFLOW (constructor_index) = 1;
8334 }
8335
8336 if (last)
8337 {
8338 if (tree_int_cst_equal (first, last))
8339 last = NULL_TREE;
8340 else if (tree_int_cst_lt (last, first))
8341 {
8342 error_init (loc, "empty index range in initializer");
8343 last = NULL_TREE;
8344 }
8345 else
8346 {
8347 last = convert (bitsizetype, last);
8348 if (constructor_max_index != NULL_TREE
8349 && tree_int_cst_lt (constructor_max_index, last))
8350 {
8351 error_init (loc, "array index range in initializer exceeds "
8352 "array bounds");
8353 last = NULL_TREE;
8354 }
8355 }
8356 }
8357
8358 designator_depth++;
8359 designator_erroneous = 0;
8360 if (constructor_range_stack || last)
8361 push_range_stack (last, braced_init_obstack);
8362 }
8363 }
8364
8365 /* Within a struct initializer, specify the next field to be initialized. */
8366
8367 void
8368 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8369 struct obstack *braced_init_obstack)
8370 {
8371 tree field;
8372
8373 if (set_designator (loc, 0, braced_init_obstack))
8374 return;
8375
8376 designator_erroneous = 1;
8377
8378 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8379 {
8380 error_init (loc, "field name not in record or union initializer");
8381 return;
8382 }
8383
8384 field = lookup_field (constructor_type, fieldname);
8385
8386 if (field == NULL_TREE)
8387 {
8388 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8389 if (guessed_id)
8390 {
8391 gcc_rich_location rich_loc (fieldname_loc);
8392 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8393 error_at_rich_loc
8394 (&rich_loc,
8395 "%qT has no member named %qE; did you mean %qE?",
8396 constructor_type, fieldname, guessed_id);
8397 }
8398 else
8399 error_at (fieldname_loc, "%qT has no member named %qE",
8400 constructor_type, fieldname);
8401 }
8402 else
8403 do
8404 {
8405 constructor_fields = TREE_VALUE (field);
8406 designator_depth++;
8407 designator_erroneous = 0;
8408 if (constructor_range_stack)
8409 push_range_stack (NULL_TREE, braced_init_obstack);
8410 field = TREE_CHAIN (field);
8411 if (field)
8412 {
8413 if (set_designator (loc, 0, braced_init_obstack))
8414 return;
8415 }
8416 }
8417 while (field != NULL_TREE);
8418 }
8419 \f
8420 /* Add a new initializer to the tree of pending initializers. PURPOSE
8421 identifies the initializer, either array index or field in a structure.
8422 VALUE is the value of that index or field. If ORIGTYPE is not
8423 NULL_TREE, it is the original type of VALUE.
8424
8425 IMPLICIT is true if value comes from pop_init_level (1),
8426 the new initializer has been merged with the existing one
8427 and thus no warnings should be emitted about overriding an
8428 existing initializer. */
8429
8430 static void
8431 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8432 bool implicit, struct obstack *braced_init_obstack)
8433 {
8434 struct init_node *p, **q, *r;
8435
8436 q = &constructor_pending_elts;
8437 p = 0;
8438
8439 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8440 {
8441 while (*q != 0)
8442 {
8443 p = *q;
8444 if (tree_int_cst_lt (purpose, p->purpose))
8445 q = &p->left;
8446 else if (tree_int_cst_lt (p->purpose, purpose))
8447 q = &p->right;
8448 else
8449 {
8450 if (!implicit)
8451 {
8452 if (TREE_SIDE_EFFECTS (p->value))
8453 warning_init (loc, OPT_Woverride_init_side_effects,
8454 "initialized field with side-effects "
8455 "overwritten");
8456 else if (warn_override_init)
8457 warning_init (loc, OPT_Woverride_init,
8458 "initialized field overwritten");
8459 }
8460 p->value = value;
8461 p->origtype = origtype;
8462 return;
8463 }
8464 }
8465 }
8466 else
8467 {
8468 tree bitpos;
8469
8470 bitpos = bit_position (purpose);
8471 while (*q != NULL)
8472 {
8473 p = *q;
8474 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8475 q = &p->left;
8476 else if (p->purpose != purpose)
8477 q = &p->right;
8478 else
8479 {
8480 if (!implicit)
8481 {
8482 if (TREE_SIDE_EFFECTS (p->value))
8483 warning_init (loc, OPT_Woverride_init_side_effects,
8484 "initialized field with side-effects "
8485 "overwritten");
8486 else if (warn_override_init)
8487 warning_init (loc, OPT_Woverride_init,
8488 "initialized field overwritten");
8489 }
8490 p->value = value;
8491 p->origtype = origtype;
8492 return;
8493 }
8494 }
8495 }
8496
8497 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8498 sizeof (struct init_node));
8499 r->purpose = purpose;
8500 r->value = value;
8501 r->origtype = origtype;
8502
8503 *q = r;
8504 r->parent = p;
8505 r->left = 0;
8506 r->right = 0;
8507 r->balance = 0;
8508
8509 while (p)
8510 {
8511 struct init_node *s;
8512
8513 if (r == p->left)
8514 {
8515 if (p->balance == 0)
8516 p->balance = -1;
8517 else if (p->balance < 0)
8518 {
8519 if (r->balance < 0)
8520 {
8521 /* L rotation. */
8522 p->left = r->right;
8523 if (p->left)
8524 p->left->parent = p;
8525 r->right = p;
8526
8527 p->balance = 0;
8528 r->balance = 0;
8529
8530 s = p->parent;
8531 p->parent = r;
8532 r->parent = s;
8533 if (s)
8534 {
8535 if (s->left == p)
8536 s->left = r;
8537 else
8538 s->right = r;
8539 }
8540 else
8541 constructor_pending_elts = r;
8542 }
8543 else
8544 {
8545 /* LR rotation. */
8546 struct init_node *t = r->right;
8547
8548 r->right = t->left;
8549 if (r->right)
8550 r->right->parent = r;
8551 t->left = r;
8552
8553 p->left = t->right;
8554 if (p->left)
8555 p->left->parent = p;
8556 t->right = p;
8557
8558 p->balance = t->balance < 0;
8559 r->balance = -(t->balance > 0);
8560 t->balance = 0;
8561
8562 s = p->parent;
8563 p->parent = t;
8564 r->parent = t;
8565 t->parent = s;
8566 if (s)
8567 {
8568 if (s->left == p)
8569 s->left = t;
8570 else
8571 s->right = t;
8572 }
8573 else
8574 constructor_pending_elts = t;
8575 }
8576 break;
8577 }
8578 else
8579 {
8580 /* p->balance == +1; growth of left side balances the node. */
8581 p->balance = 0;
8582 break;
8583 }
8584 }
8585 else /* r == p->right */
8586 {
8587 if (p->balance == 0)
8588 /* Growth propagation from right side. */
8589 p->balance++;
8590 else if (p->balance > 0)
8591 {
8592 if (r->balance > 0)
8593 {
8594 /* R rotation. */
8595 p->right = r->left;
8596 if (p->right)
8597 p->right->parent = p;
8598 r->left = p;
8599
8600 p->balance = 0;
8601 r->balance = 0;
8602
8603 s = p->parent;
8604 p->parent = r;
8605 r->parent = s;
8606 if (s)
8607 {
8608 if (s->left == p)
8609 s->left = r;
8610 else
8611 s->right = r;
8612 }
8613 else
8614 constructor_pending_elts = r;
8615 }
8616 else /* r->balance == -1 */
8617 {
8618 /* RL rotation */
8619 struct init_node *t = r->left;
8620
8621 r->left = t->right;
8622 if (r->left)
8623 r->left->parent = r;
8624 t->right = r;
8625
8626 p->right = t->left;
8627 if (p->right)
8628 p->right->parent = p;
8629 t->left = p;
8630
8631 r->balance = (t->balance < 0);
8632 p->balance = -(t->balance > 0);
8633 t->balance = 0;
8634
8635 s = p->parent;
8636 p->parent = t;
8637 r->parent = t;
8638 t->parent = s;
8639 if (s)
8640 {
8641 if (s->left == p)
8642 s->left = t;
8643 else
8644 s->right = t;
8645 }
8646 else
8647 constructor_pending_elts = t;
8648 }
8649 break;
8650 }
8651 else
8652 {
8653 /* p->balance == -1; growth of right side balances the node. */
8654 p->balance = 0;
8655 break;
8656 }
8657 }
8658
8659 r = p;
8660 p = p->parent;
8661 }
8662 }
8663
8664 /* Build AVL tree from a sorted chain. */
8665
8666 static void
8667 set_nonincremental_init (struct obstack * braced_init_obstack)
8668 {
8669 unsigned HOST_WIDE_INT ix;
8670 tree index, value;
8671
8672 if (TREE_CODE (constructor_type) != RECORD_TYPE
8673 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8674 return;
8675
8676 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8677 add_pending_init (input_location, index, value, NULL_TREE, true,
8678 braced_init_obstack);
8679 constructor_elements = NULL;
8680 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8681 {
8682 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8683 /* Skip any nameless bit fields at the beginning. */
8684 while (constructor_unfilled_fields != NULL_TREE
8685 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8686 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
8687 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8688
8689 }
8690 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8691 {
8692 if (TYPE_DOMAIN (constructor_type))
8693 constructor_unfilled_index
8694 = convert (bitsizetype,
8695 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8696 else
8697 constructor_unfilled_index = bitsize_zero_node;
8698 }
8699 constructor_incremental = 0;
8700 }
8701
8702 /* Build AVL tree from a string constant. */
8703
8704 static void
8705 set_nonincremental_init_from_string (tree str,
8706 struct obstack * braced_init_obstack)
8707 {
8708 tree value, purpose, type;
8709 HOST_WIDE_INT val[2];
8710 const char *p, *end;
8711 int byte, wchar_bytes, charwidth, bitpos;
8712
8713 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8714
8715 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8716 charwidth = TYPE_PRECISION (char_type_node);
8717 gcc_assert ((size_t) wchar_bytes * charwidth
8718 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
8719 type = TREE_TYPE (constructor_type);
8720 p = TREE_STRING_POINTER (str);
8721 end = p + TREE_STRING_LENGTH (str);
8722
8723 for (purpose = bitsize_zero_node;
8724 p < end
8725 && !(constructor_max_index
8726 && tree_int_cst_lt (constructor_max_index, purpose));
8727 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8728 {
8729 if (wchar_bytes == 1)
8730 {
8731 val[0] = (unsigned char) *p++;
8732 val[1] = 0;
8733 }
8734 else
8735 {
8736 val[1] = 0;
8737 val[0] = 0;
8738 for (byte = 0; byte < wchar_bytes; byte++)
8739 {
8740 if (BYTES_BIG_ENDIAN)
8741 bitpos = (wchar_bytes - byte - 1) * charwidth;
8742 else
8743 bitpos = byte * charwidth;
8744 val[bitpos / HOST_BITS_PER_WIDE_INT]
8745 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8746 << (bitpos % HOST_BITS_PER_WIDE_INT);
8747 }
8748 }
8749
8750 if (!TYPE_UNSIGNED (type))
8751 {
8752 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8753 if (bitpos < HOST_BITS_PER_WIDE_INT)
8754 {
8755 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
8756 {
8757 val[0] |= HOST_WIDE_INT_M1U << bitpos;
8758 val[1] = -1;
8759 }
8760 }
8761 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8762 {
8763 if (val[0] < 0)
8764 val[1] = -1;
8765 }
8766 else if (val[1] & (HOST_WIDE_INT_1
8767 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8768 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
8769 }
8770
8771 value = wide_int_to_tree (type,
8772 wide_int::from_array (val, 2,
8773 HOST_BITS_PER_WIDE_INT * 2));
8774 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8775 braced_init_obstack);
8776 }
8777
8778 constructor_incremental = 0;
8779 }
8780
8781 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
8782 not initialized yet. */
8783
8784 static tree
8785 find_init_member (tree field, struct obstack * braced_init_obstack)
8786 {
8787 struct init_node *p;
8788
8789 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8790 {
8791 if (constructor_incremental
8792 && tree_int_cst_lt (field, constructor_unfilled_index))
8793 set_nonincremental_init (braced_init_obstack);
8794
8795 p = constructor_pending_elts;
8796 while (p)
8797 {
8798 if (tree_int_cst_lt (field, p->purpose))
8799 p = p->left;
8800 else if (tree_int_cst_lt (p->purpose, field))
8801 p = p->right;
8802 else
8803 return p->value;
8804 }
8805 }
8806 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8807 {
8808 tree bitpos = bit_position (field);
8809
8810 if (constructor_incremental
8811 && (!constructor_unfilled_fields
8812 || tree_int_cst_lt (bitpos,
8813 bit_position (constructor_unfilled_fields))))
8814 set_nonincremental_init (braced_init_obstack);
8815
8816 p = constructor_pending_elts;
8817 while (p)
8818 {
8819 if (field == p->purpose)
8820 return p->value;
8821 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8822 p = p->left;
8823 else
8824 p = p->right;
8825 }
8826 }
8827 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8828 {
8829 if (!vec_safe_is_empty (constructor_elements)
8830 && (constructor_elements->last ().index == field))
8831 return constructor_elements->last ().value;
8832 }
8833 return NULL_TREE;
8834 }
8835
8836 /* "Output" the next constructor element.
8837 At top level, really output it to assembler code now.
8838 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8839 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8840 TYPE is the data type that the containing data type wants here.
8841 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8842 If VALUE is a string constant, STRICT_STRING is true if it is
8843 unparenthesized or we should not warn here for it being parenthesized.
8844 For other types of VALUE, STRICT_STRING is not used.
8845
8846 PENDING if non-nil means output pending elements that belong
8847 right after this element. (PENDING is normally 1;
8848 it is 0 while outputting pending elements, to avoid recursion.)
8849
8850 IMPLICIT is true if value comes from pop_init_level (1),
8851 the new initializer has been merged with the existing one
8852 and thus no warnings should be emitted about overriding an
8853 existing initializer. */
8854
8855 static void
8856 output_init_element (location_t loc, tree value, tree origtype,
8857 bool strict_string, tree type, tree field, int pending,
8858 bool implicit, struct obstack * braced_init_obstack)
8859 {
8860 tree semantic_type = NULL_TREE;
8861 bool maybe_const = true;
8862 bool npc;
8863
8864 if (type == error_mark_node || value == error_mark_node)
8865 {
8866 constructor_erroneous = 1;
8867 return;
8868 }
8869 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8870 && (TREE_CODE (value) == STRING_CST
8871 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8872 && !(TREE_CODE (value) == STRING_CST
8873 && TREE_CODE (type) == ARRAY_TYPE
8874 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8875 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8876 TYPE_MAIN_VARIANT (type)))
8877 value = array_to_pointer_conversion (input_location, value);
8878
8879 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8880 && require_constant_value && pending)
8881 {
8882 /* As an extension, allow initializing objects with static storage
8883 duration with compound literals (which are then treated just as
8884 the brace enclosed list they contain). */
8885 if (flag_isoc99)
8886 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8887 "constant");
8888 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8889 value = DECL_INITIAL (decl);
8890 }
8891
8892 npc = null_pointer_constant_p (value);
8893 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8894 {
8895 semantic_type = TREE_TYPE (value);
8896 value = TREE_OPERAND (value, 0);
8897 }
8898 value = c_fully_fold (value, require_constant_value, &maybe_const);
8899
8900 if (value == error_mark_node)
8901 constructor_erroneous = 1;
8902 else if (!TREE_CONSTANT (value))
8903 constructor_constant = 0;
8904 else if (!initializer_constant_valid_p (value,
8905 TREE_TYPE (value),
8906 AGGREGATE_TYPE_P (constructor_type)
8907 && TYPE_REVERSE_STORAGE_ORDER
8908 (constructor_type))
8909 || (RECORD_OR_UNION_TYPE_P (constructor_type)
8910 && DECL_C_BIT_FIELD (field)
8911 && TREE_CODE (value) != INTEGER_CST))
8912 constructor_simple = 0;
8913 if (!maybe_const)
8914 constructor_nonconst = 1;
8915
8916 /* Digest the initializer and issue any errors about incompatible
8917 types before issuing errors about non-constant initializers. */
8918 tree new_value = value;
8919 if (semantic_type)
8920 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8921 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
8922 require_constant_value);
8923 if (new_value == error_mark_node)
8924 {
8925 constructor_erroneous = 1;
8926 return;
8927 }
8928 if (require_constant_value || require_constant_elements)
8929 constant_expression_warning (new_value);
8930
8931 /* Proceed to check the constness of the original initializer. */
8932 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8933 {
8934 if (require_constant_value)
8935 {
8936 error_init (loc, "initializer element is not constant");
8937 value = error_mark_node;
8938 }
8939 else if (require_constant_elements)
8940 pedwarn (loc, OPT_Wpedantic,
8941 "initializer element is not computable at load time");
8942 }
8943 else if (!maybe_const
8944 && (require_constant_value || require_constant_elements))
8945 pedwarn_init (loc, OPT_Wpedantic,
8946 "initializer element is not a constant expression");
8947
8948 /* Issue -Wc++-compat warnings about initializing a bitfield with
8949 enum type. */
8950 if (warn_cxx_compat
8951 && field != NULL_TREE
8952 && TREE_CODE (field) == FIELD_DECL
8953 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8954 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8955 != TYPE_MAIN_VARIANT (type))
8956 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8957 {
8958 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8959 if (checktype != error_mark_node
8960 && (TYPE_MAIN_VARIANT (checktype)
8961 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8962 warning_init (loc, OPT_Wc___compat,
8963 "enum conversion in initialization is invalid in C++");
8964 }
8965
8966 /* If this field is empty (and not at the end of structure),
8967 don't do anything other than checking the initializer. */
8968 if (field
8969 && (TREE_TYPE (field) == error_mark_node
8970 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8971 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8972 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8973 || DECL_CHAIN (field)))))
8974 return;
8975
8976 /* Finally, set VALUE to the initializer value digested above. */
8977 value = new_value;
8978
8979 /* If this element doesn't come next in sequence,
8980 put it on constructor_pending_elts. */
8981 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8982 && (!constructor_incremental
8983 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8984 {
8985 if (constructor_incremental
8986 && tree_int_cst_lt (field, constructor_unfilled_index))
8987 set_nonincremental_init (braced_init_obstack);
8988
8989 add_pending_init (loc, field, value, origtype, implicit,
8990 braced_init_obstack);
8991 return;
8992 }
8993 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8994 && (!constructor_incremental
8995 || field != constructor_unfilled_fields))
8996 {
8997 /* We do this for records but not for unions. In a union,
8998 no matter which field is specified, it can be initialized
8999 right away since it starts at the beginning of the union. */
9000 if (constructor_incremental)
9001 {
9002 if (!constructor_unfilled_fields)
9003 set_nonincremental_init (braced_init_obstack);
9004 else
9005 {
9006 tree bitpos, unfillpos;
9007
9008 bitpos = bit_position (field);
9009 unfillpos = bit_position (constructor_unfilled_fields);
9010
9011 if (tree_int_cst_lt (bitpos, unfillpos))
9012 set_nonincremental_init (braced_init_obstack);
9013 }
9014 }
9015
9016 add_pending_init (loc, field, value, origtype, implicit,
9017 braced_init_obstack);
9018 return;
9019 }
9020 else if (TREE_CODE (constructor_type) == UNION_TYPE
9021 && !vec_safe_is_empty (constructor_elements))
9022 {
9023 if (!implicit)
9024 {
9025 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9026 warning_init (loc, OPT_Woverride_init_side_effects,
9027 "initialized field with side-effects overwritten");
9028 else if (warn_override_init)
9029 warning_init (loc, OPT_Woverride_init,
9030 "initialized field overwritten");
9031 }
9032
9033 /* We can have just one union field set. */
9034 constructor_elements = NULL;
9035 }
9036
9037 /* Otherwise, output this element either to
9038 constructor_elements or to the assembler file. */
9039
9040 constructor_elt celt = {field, value};
9041 vec_safe_push (constructor_elements, celt);
9042
9043 /* Advance the variable that indicates sequential elements output. */
9044 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9045 constructor_unfilled_index
9046 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9047 bitsize_one_node);
9048 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9049 {
9050 constructor_unfilled_fields
9051 = DECL_CHAIN (constructor_unfilled_fields);
9052
9053 /* Skip any nameless bit fields. */
9054 while (constructor_unfilled_fields != NULL_TREE
9055 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9056 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
9057 constructor_unfilled_fields =
9058 DECL_CHAIN (constructor_unfilled_fields);
9059 }
9060 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9061 constructor_unfilled_fields = NULL_TREE;
9062
9063 /* Now output any pending elements which have become next. */
9064 if (pending)
9065 output_pending_init_elements (0, braced_init_obstack);
9066 }
9067
9068 /* Output any pending elements which have become next.
9069 As we output elements, constructor_unfilled_{fields,index}
9070 advances, which may cause other elements to become next;
9071 if so, they too are output.
9072
9073 If ALL is 0, we return when there are
9074 no more pending elements to output now.
9075
9076 If ALL is 1, we output space as necessary so that
9077 we can output all the pending elements. */
9078 static void
9079 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9080 {
9081 struct init_node *elt = constructor_pending_elts;
9082 tree next;
9083
9084 retry:
9085
9086 /* Look through the whole pending tree.
9087 If we find an element that should be output now,
9088 output it. Otherwise, set NEXT to the element
9089 that comes first among those still pending. */
9090
9091 next = NULL_TREE;
9092 while (elt)
9093 {
9094 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9095 {
9096 if (tree_int_cst_equal (elt->purpose,
9097 constructor_unfilled_index))
9098 output_init_element (input_location, elt->value, elt->origtype,
9099 true, TREE_TYPE (constructor_type),
9100 constructor_unfilled_index, 0, false,
9101 braced_init_obstack);
9102 else if (tree_int_cst_lt (constructor_unfilled_index,
9103 elt->purpose))
9104 {
9105 /* Advance to the next smaller node. */
9106 if (elt->left)
9107 elt = elt->left;
9108 else
9109 {
9110 /* We have reached the smallest node bigger than the
9111 current unfilled index. Fill the space first. */
9112 next = elt->purpose;
9113 break;
9114 }
9115 }
9116 else
9117 {
9118 /* Advance to the next bigger node. */
9119 if (elt->right)
9120 elt = elt->right;
9121 else
9122 {
9123 /* We have reached the biggest node in a subtree. Find
9124 the parent of it, which is the next bigger node. */
9125 while (elt->parent && elt->parent->right == elt)
9126 elt = elt->parent;
9127 elt = elt->parent;
9128 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9129 elt->purpose))
9130 {
9131 next = elt->purpose;
9132 break;
9133 }
9134 }
9135 }
9136 }
9137 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9138 {
9139 tree ctor_unfilled_bitpos, elt_bitpos;
9140
9141 /* If the current record is complete we are done. */
9142 if (constructor_unfilled_fields == NULL_TREE)
9143 break;
9144
9145 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9146 elt_bitpos = bit_position (elt->purpose);
9147 /* We can't compare fields here because there might be empty
9148 fields in between. */
9149 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9150 {
9151 constructor_unfilled_fields = elt->purpose;
9152 output_init_element (input_location, elt->value, elt->origtype,
9153 true, TREE_TYPE (elt->purpose),
9154 elt->purpose, 0, false,
9155 braced_init_obstack);
9156 }
9157 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9158 {
9159 /* Advance to the next smaller node. */
9160 if (elt->left)
9161 elt = elt->left;
9162 else
9163 {
9164 /* We have reached the smallest node bigger than the
9165 current unfilled field. Fill the space first. */
9166 next = elt->purpose;
9167 break;
9168 }
9169 }
9170 else
9171 {
9172 /* Advance to the next bigger node. */
9173 if (elt->right)
9174 elt = elt->right;
9175 else
9176 {
9177 /* We have reached the biggest node in a subtree. Find
9178 the parent of it, which is the next bigger node. */
9179 while (elt->parent && elt->parent->right == elt)
9180 elt = elt->parent;
9181 elt = elt->parent;
9182 if (elt
9183 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9184 bit_position (elt->purpose))))
9185 {
9186 next = elt->purpose;
9187 break;
9188 }
9189 }
9190 }
9191 }
9192 }
9193
9194 /* Ordinarily return, but not if we want to output all
9195 and there are elements left. */
9196 if (!(all && next != NULL_TREE))
9197 return;
9198
9199 /* If it's not incremental, just skip over the gap, so that after
9200 jumping to retry we will output the next successive element. */
9201 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9202 constructor_unfilled_fields = next;
9203 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9204 constructor_unfilled_index = next;
9205
9206 /* ELT now points to the node in the pending tree with the next
9207 initializer to output. */
9208 goto retry;
9209 }
9210 \f
9211 /* Add one non-braced element to the current constructor level.
9212 This adjusts the current position within the constructor's type.
9213 This may also start or terminate implicit levels
9214 to handle a partly-braced initializer.
9215
9216 Once this has found the correct level for the new element,
9217 it calls output_init_element.
9218
9219 IMPLICIT is true if value comes from pop_init_level (1),
9220 the new initializer has been merged with the existing one
9221 and thus no warnings should be emitted about overriding an
9222 existing initializer. */
9223
9224 void
9225 process_init_element (location_t loc, struct c_expr value, bool implicit,
9226 struct obstack * braced_init_obstack)
9227 {
9228 tree orig_value = value.value;
9229 int string_flag
9230 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
9231 bool strict_string = value.original_code == STRING_CST;
9232 bool was_designated = designator_depth != 0;
9233
9234 designator_depth = 0;
9235 designator_erroneous = 0;
9236
9237 if (!implicit && value.value && !integer_zerop (value.value))
9238 constructor_zeroinit = 0;
9239
9240 /* Handle superfluous braces around string cst as in
9241 char x[] = {"foo"}; */
9242 if (string_flag
9243 && constructor_type
9244 && !was_designated
9245 && TREE_CODE (constructor_type) == ARRAY_TYPE
9246 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9247 && integer_zerop (constructor_unfilled_index))
9248 {
9249 if (constructor_stack->replacement_value.value)
9250 error_init (loc, "excess elements in char array initializer");
9251 constructor_stack->replacement_value = value;
9252 return;
9253 }
9254
9255 if (constructor_stack->replacement_value.value != NULL_TREE)
9256 {
9257 error_init (loc, "excess elements in struct initializer");
9258 return;
9259 }
9260
9261 /* Ignore elements of a brace group if it is entirely superfluous
9262 and has already been diagnosed. */
9263 if (constructor_type == NULL_TREE)
9264 return;
9265
9266 if (!implicit && warn_designated_init && !was_designated
9267 && TREE_CODE (constructor_type) == RECORD_TYPE
9268 && lookup_attribute ("designated_init",
9269 TYPE_ATTRIBUTES (constructor_type)))
9270 warning_init (loc,
9271 OPT_Wdesignated_init,
9272 "positional initialization of field "
9273 "in %<struct%> declared with %<designated_init%> attribute");
9274
9275 /* If we've exhausted any levels that didn't have braces,
9276 pop them now. */
9277 while (constructor_stack->implicit)
9278 {
9279 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9280 && constructor_fields == NULL_TREE)
9281 process_init_element (loc,
9282 pop_init_level (loc, 1, braced_init_obstack,
9283 last_init_list_comma),
9284 true, braced_init_obstack);
9285 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9286 || VECTOR_TYPE_P (constructor_type))
9287 && constructor_max_index
9288 && tree_int_cst_lt (constructor_max_index,
9289 constructor_index))
9290 process_init_element (loc,
9291 pop_init_level (loc, 1, braced_init_obstack,
9292 last_init_list_comma),
9293 true, braced_init_obstack);
9294 else
9295 break;
9296 }
9297
9298 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9299 if (constructor_range_stack)
9300 {
9301 /* If value is a compound literal and we'll be just using its
9302 content, don't put it into a SAVE_EXPR. */
9303 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9304 || !require_constant_value)
9305 {
9306 tree semantic_type = NULL_TREE;
9307 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9308 {
9309 semantic_type = TREE_TYPE (value.value);
9310 value.value = TREE_OPERAND (value.value, 0);
9311 }
9312 value.value = save_expr (value.value);
9313 if (semantic_type)
9314 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9315 value.value);
9316 }
9317 }
9318
9319 while (1)
9320 {
9321 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9322 {
9323 tree fieldtype;
9324 enum tree_code fieldcode;
9325
9326 if (constructor_fields == NULL_TREE)
9327 {
9328 pedwarn_init (loc, 0, "excess elements in struct initializer");
9329 break;
9330 }
9331
9332 fieldtype = TREE_TYPE (constructor_fields);
9333 if (fieldtype != error_mark_node)
9334 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9335 fieldcode = TREE_CODE (fieldtype);
9336
9337 /* Error for non-static initialization of a flexible array member. */
9338 if (fieldcode == ARRAY_TYPE
9339 && !require_constant_value
9340 && TYPE_SIZE (fieldtype) == NULL_TREE
9341 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9342 {
9343 error_init (loc, "non-static initialization of a flexible "
9344 "array member");
9345 break;
9346 }
9347
9348 /* Error for initialization of a flexible array member with
9349 a string constant if the structure is in an array. E.g.:
9350 struct S { int x; char y[]; };
9351 struct S s[] = { { 1, "foo" } };
9352 is invalid. */
9353 if (string_flag
9354 && fieldcode == ARRAY_TYPE
9355 && constructor_depth > 1
9356 && TYPE_SIZE (fieldtype) == NULL_TREE
9357 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9358 {
9359 bool in_array_p = false;
9360 for (struct constructor_stack *p = constructor_stack;
9361 p && p->type; p = p->next)
9362 if (TREE_CODE (p->type) == ARRAY_TYPE)
9363 {
9364 in_array_p = true;
9365 break;
9366 }
9367 if (in_array_p)
9368 {
9369 error_init (loc, "initialization of flexible array "
9370 "member in a nested context");
9371 break;
9372 }
9373 }
9374
9375 /* Accept a string constant to initialize a subarray. */
9376 if (value.value != NULL_TREE
9377 && fieldcode == ARRAY_TYPE
9378 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9379 && string_flag)
9380 value.value = orig_value;
9381 /* Otherwise, if we have come to a subaggregate,
9382 and we don't have an element of its type, push into it. */
9383 else if (value.value != NULL_TREE
9384 && value.value != error_mark_node
9385 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9386 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9387 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9388 {
9389 push_init_level (loc, 1, braced_init_obstack);
9390 continue;
9391 }
9392
9393 if (value.value)
9394 {
9395 push_member_name (constructor_fields);
9396 output_init_element (loc, value.value, value.original_type,
9397 strict_string, fieldtype,
9398 constructor_fields, 1, implicit,
9399 braced_init_obstack);
9400 RESTORE_SPELLING_DEPTH (constructor_depth);
9401 }
9402 else
9403 /* Do the bookkeeping for an element that was
9404 directly output as a constructor. */
9405 {
9406 /* For a record, keep track of end position of last field. */
9407 if (DECL_SIZE (constructor_fields))
9408 constructor_bit_index
9409 = size_binop_loc (input_location, PLUS_EXPR,
9410 bit_position (constructor_fields),
9411 DECL_SIZE (constructor_fields));
9412
9413 /* If the current field was the first one not yet written out,
9414 it isn't now, so update. */
9415 if (constructor_unfilled_fields == constructor_fields)
9416 {
9417 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9418 /* Skip any nameless bit fields. */
9419 while (constructor_unfilled_fields != 0
9420 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9421 && DECL_NAME (constructor_unfilled_fields) == 0)
9422 constructor_unfilled_fields =
9423 DECL_CHAIN (constructor_unfilled_fields);
9424 }
9425 }
9426
9427 constructor_fields = DECL_CHAIN (constructor_fields);
9428 /* Skip any nameless bit fields at the beginning. */
9429 while (constructor_fields != NULL_TREE
9430 && DECL_C_BIT_FIELD (constructor_fields)
9431 && DECL_NAME (constructor_fields) == NULL_TREE)
9432 constructor_fields = DECL_CHAIN (constructor_fields);
9433 }
9434 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9435 {
9436 tree fieldtype;
9437 enum tree_code fieldcode;
9438
9439 if (constructor_fields == NULL_TREE)
9440 {
9441 pedwarn_init (loc, 0,
9442 "excess elements in union initializer");
9443 break;
9444 }
9445
9446 fieldtype = TREE_TYPE (constructor_fields);
9447 if (fieldtype != error_mark_node)
9448 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9449 fieldcode = TREE_CODE (fieldtype);
9450
9451 /* Warn that traditional C rejects initialization of unions.
9452 We skip the warning if the value is zero. This is done
9453 under the assumption that the zero initializer in user
9454 code appears conditioned on e.g. __STDC__ to avoid
9455 "missing initializer" warnings and relies on default
9456 initialization to zero in the traditional C case.
9457 We also skip the warning if the initializer is designated,
9458 again on the assumption that this must be conditional on
9459 __STDC__ anyway (and we've already complained about the
9460 member-designator already). */
9461 if (!in_system_header_at (input_location) && !constructor_designated
9462 && !(value.value && (integer_zerop (value.value)
9463 || real_zerop (value.value))))
9464 warning (OPT_Wtraditional, "traditional C rejects initialization "
9465 "of unions");
9466
9467 /* Accept a string constant to initialize a subarray. */
9468 if (value.value != NULL_TREE
9469 && fieldcode == ARRAY_TYPE
9470 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9471 && string_flag)
9472 value.value = orig_value;
9473 /* Otherwise, if we have come to a subaggregate,
9474 and we don't have an element of its type, push into it. */
9475 else if (value.value != NULL_TREE
9476 && value.value != error_mark_node
9477 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9478 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9479 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9480 {
9481 push_init_level (loc, 1, braced_init_obstack);
9482 continue;
9483 }
9484
9485 if (value.value)
9486 {
9487 push_member_name (constructor_fields);
9488 output_init_element (loc, value.value, value.original_type,
9489 strict_string, fieldtype,
9490 constructor_fields, 1, implicit,
9491 braced_init_obstack);
9492 RESTORE_SPELLING_DEPTH (constructor_depth);
9493 }
9494 else
9495 /* Do the bookkeeping for an element that was
9496 directly output as a constructor. */
9497 {
9498 constructor_bit_index = DECL_SIZE (constructor_fields);
9499 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9500 }
9501
9502 constructor_fields = NULL_TREE;
9503 }
9504 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9505 {
9506 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9507 enum tree_code eltcode = TREE_CODE (elttype);
9508
9509 /* Accept a string constant to initialize a subarray. */
9510 if (value.value != NULL_TREE
9511 && eltcode == ARRAY_TYPE
9512 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9513 && string_flag)
9514 value.value = orig_value;
9515 /* Otherwise, if we have come to a subaggregate,
9516 and we don't have an element of its type, push into it. */
9517 else if (value.value != NULL_TREE
9518 && value.value != error_mark_node
9519 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9520 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9521 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9522 {
9523 push_init_level (loc, 1, braced_init_obstack);
9524 continue;
9525 }
9526
9527 if (constructor_max_index != NULL_TREE
9528 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9529 || integer_all_onesp (constructor_max_index)))
9530 {
9531 pedwarn_init (loc, 0,
9532 "excess elements in array initializer");
9533 break;
9534 }
9535
9536 /* Now output the actual element. */
9537 if (value.value)
9538 {
9539 push_array_bounds (tree_to_uhwi (constructor_index));
9540 output_init_element (loc, value.value, value.original_type,
9541 strict_string, elttype,
9542 constructor_index, 1, implicit,
9543 braced_init_obstack);
9544 RESTORE_SPELLING_DEPTH (constructor_depth);
9545 }
9546
9547 constructor_index
9548 = size_binop_loc (input_location, PLUS_EXPR,
9549 constructor_index, bitsize_one_node);
9550
9551 if (!value.value)
9552 /* If we are doing the bookkeeping for an element that was
9553 directly output as a constructor, we must update
9554 constructor_unfilled_index. */
9555 constructor_unfilled_index = constructor_index;
9556 }
9557 else if (VECTOR_TYPE_P (constructor_type))
9558 {
9559 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9560
9561 /* Do a basic check of initializer size. Note that vectors
9562 always have a fixed size derived from their type. */
9563 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9564 {
9565 pedwarn_init (loc, 0,
9566 "excess elements in vector initializer");
9567 break;
9568 }
9569
9570 /* Now output the actual element. */
9571 if (value.value)
9572 {
9573 if (TREE_CODE (value.value) == VECTOR_CST)
9574 elttype = TYPE_MAIN_VARIANT (constructor_type);
9575 output_init_element (loc, value.value, value.original_type,
9576 strict_string, elttype,
9577 constructor_index, 1, implicit,
9578 braced_init_obstack);
9579 }
9580
9581 constructor_index
9582 = size_binop_loc (input_location,
9583 PLUS_EXPR, constructor_index, bitsize_one_node);
9584
9585 if (!value.value)
9586 /* If we are doing the bookkeeping for an element that was
9587 directly output as a constructor, we must update
9588 constructor_unfilled_index. */
9589 constructor_unfilled_index = constructor_index;
9590 }
9591
9592 /* Handle the sole element allowed in a braced initializer
9593 for a scalar variable. */
9594 else if (constructor_type != error_mark_node
9595 && constructor_fields == NULL_TREE)
9596 {
9597 pedwarn_init (loc, 0,
9598 "excess elements in scalar initializer");
9599 break;
9600 }
9601 else
9602 {
9603 if (value.value)
9604 output_init_element (loc, value.value, value.original_type,
9605 strict_string, constructor_type,
9606 NULL_TREE, 1, implicit,
9607 braced_init_obstack);
9608 constructor_fields = NULL_TREE;
9609 }
9610
9611 /* Handle range initializers either at this level or anywhere higher
9612 in the designator stack. */
9613 if (constructor_range_stack)
9614 {
9615 struct constructor_range_stack *p, *range_stack;
9616 int finish = 0;
9617
9618 range_stack = constructor_range_stack;
9619 constructor_range_stack = 0;
9620 while (constructor_stack != range_stack->stack)
9621 {
9622 gcc_assert (constructor_stack->implicit);
9623 process_init_element (loc,
9624 pop_init_level (loc, 1,
9625 braced_init_obstack,
9626 last_init_list_comma),
9627 true, braced_init_obstack);
9628 }
9629 for (p = range_stack;
9630 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9631 p = p->prev)
9632 {
9633 gcc_assert (constructor_stack->implicit);
9634 process_init_element (loc,
9635 pop_init_level (loc, 1,
9636 braced_init_obstack,
9637 last_init_list_comma),
9638 true, braced_init_obstack);
9639 }
9640
9641 p->index = size_binop_loc (input_location,
9642 PLUS_EXPR, p->index, bitsize_one_node);
9643 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9644 finish = 1;
9645
9646 while (1)
9647 {
9648 constructor_index = p->index;
9649 constructor_fields = p->fields;
9650 if (finish && p->range_end && p->index == p->range_start)
9651 {
9652 finish = 0;
9653 p->prev = 0;
9654 }
9655 p = p->next;
9656 if (!p)
9657 break;
9658 finish_implicit_inits (loc, braced_init_obstack);
9659 push_init_level (loc, 2, braced_init_obstack);
9660 p->stack = constructor_stack;
9661 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9662 p->index = p->range_start;
9663 }
9664
9665 if (!finish)
9666 constructor_range_stack = range_stack;
9667 continue;
9668 }
9669
9670 break;
9671 }
9672
9673 constructor_range_stack = 0;
9674 }
9675 \f
9676 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9677 (guaranteed to be 'volatile' or null) and ARGS (represented using
9678 an ASM_EXPR node). */
9679 tree
9680 build_asm_stmt (tree cv_qualifier, tree args)
9681 {
9682 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9683 ASM_VOLATILE_P (args) = 1;
9684 return add_stmt (args);
9685 }
9686
9687 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9688 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9689 SIMPLE indicates whether there was anything at all after the
9690 string in the asm expression -- asm("blah") and asm("blah" : )
9691 are subtly different. We use a ASM_EXPR node to represent this. */
9692 tree
9693 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9694 tree clobbers, tree labels, bool simple)
9695 {
9696 tree tail;
9697 tree args;
9698 int i;
9699 const char *constraint;
9700 const char **oconstraints;
9701 bool allows_mem, allows_reg, is_inout;
9702 int ninputs, noutputs;
9703
9704 ninputs = list_length (inputs);
9705 noutputs = list_length (outputs);
9706 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9707
9708 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9709
9710 /* Remove output conversions that change the type but not the mode. */
9711 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9712 {
9713 tree output = TREE_VALUE (tail);
9714
9715 output = c_fully_fold (output, false, NULL);
9716
9717 /* ??? Really, this should not be here. Users should be using a
9718 proper lvalue, dammit. But there's a long history of using casts
9719 in the output operands. In cases like longlong.h, this becomes a
9720 primitive form of typechecking -- if the cast can be removed, then
9721 the output operand had a type of the proper width; otherwise we'll
9722 get an error. Gross, but ... */
9723 STRIP_NOPS (output);
9724
9725 if (!lvalue_or_else (loc, output, lv_asm))
9726 output = error_mark_node;
9727
9728 if (output != error_mark_node
9729 && (TREE_READONLY (output)
9730 || TYPE_READONLY (TREE_TYPE (output))
9731 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
9732 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9733 readonly_error (loc, output, lv_asm);
9734
9735 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9736 oconstraints[i] = constraint;
9737
9738 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9739 &allows_mem, &allows_reg, &is_inout))
9740 {
9741 /* If the operand is going to end up in memory,
9742 mark it addressable. */
9743 if (!allows_reg && !c_mark_addressable (output))
9744 output = error_mark_node;
9745 if (!(!allows_reg && allows_mem)
9746 && output != error_mark_node
9747 && VOID_TYPE_P (TREE_TYPE (output)))
9748 {
9749 error_at (loc, "invalid use of void expression");
9750 output = error_mark_node;
9751 }
9752 }
9753 else
9754 output = error_mark_node;
9755
9756 TREE_VALUE (tail) = output;
9757 }
9758
9759 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9760 {
9761 tree input;
9762
9763 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9764 input = TREE_VALUE (tail);
9765
9766 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9767 oconstraints, &allows_mem, &allows_reg))
9768 {
9769 /* If the operand is going to end up in memory,
9770 mark it addressable. */
9771 if (!allows_reg && allows_mem)
9772 {
9773 input = c_fully_fold (input, false, NULL);
9774
9775 /* Strip the nops as we allow this case. FIXME, this really
9776 should be rejected or made deprecated. */
9777 STRIP_NOPS (input);
9778 if (!c_mark_addressable (input))
9779 input = error_mark_node;
9780 }
9781 else
9782 {
9783 struct c_expr expr;
9784 memset (&expr, 0, sizeof (expr));
9785 expr.value = input;
9786 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9787 input = c_fully_fold (expr.value, false, NULL);
9788
9789 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9790 {
9791 error_at (loc, "invalid use of void expression");
9792 input = error_mark_node;
9793 }
9794 }
9795 }
9796 else
9797 input = error_mark_node;
9798
9799 TREE_VALUE (tail) = input;
9800 }
9801
9802 /* ASMs with labels cannot have outputs. This should have been
9803 enforced by the parser. */
9804 gcc_assert (outputs == NULL || labels == NULL);
9805
9806 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9807
9808 /* asm statements without outputs, including simple ones, are treated
9809 as volatile. */
9810 ASM_INPUT_P (args) = simple;
9811 ASM_VOLATILE_P (args) = (noutputs == 0);
9812
9813 return args;
9814 }
9815 \f
9816 /* Generate a goto statement to LABEL. LOC is the location of the
9817 GOTO. */
9818
9819 tree
9820 c_finish_goto_label (location_t loc, tree label)
9821 {
9822 tree decl = lookup_label_for_goto (loc, label);
9823 if (!decl)
9824 return NULL_TREE;
9825 TREE_USED (decl) = 1;
9826 {
9827 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9828 SET_EXPR_LOCATION (t, loc);
9829 return add_stmt (t);
9830 }
9831 }
9832
9833 /* Generate a computed goto statement to EXPR. LOC is the location of
9834 the GOTO. */
9835
9836 tree
9837 c_finish_goto_ptr (location_t loc, tree expr)
9838 {
9839 tree t;
9840 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9841 expr = c_fully_fold (expr, false, NULL);
9842 expr = convert (ptr_type_node, expr);
9843 t = build1 (GOTO_EXPR, void_type_node, expr);
9844 SET_EXPR_LOCATION (t, loc);
9845 return add_stmt (t);
9846 }
9847
9848 /* Generate a C `return' statement. RETVAL is the expression for what
9849 to return, or a null pointer for `return;' with no value. LOC is
9850 the location of the return statement, or the location of the expression,
9851 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9852 is the original type of RETVAL. */
9853
9854 tree
9855 c_finish_return (location_t loc, tree retval, tree origtype)
9856 {
9857 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9858 bool no_warning = false;
9859 bool npc = false;
9860 size_t rank = 0;
9861
9862 /* Use the expansion point to handle cases such as returning NULL
9863 in a function returning void. */
9864 source_location xloc = expansion_point_location_if_in_system_header (loc);
9865
9866 if (TREE_THIS_VOLATILE (current_function_decl))
9867 warning_at (xloc, 0,
9868 "function declared %<noreturn%> has a %<return%> statement");
9869
9870 if (flag_cilkplus && contains_array_notation_expr (retval))
9871 {
9872 /* Array notations are allowed in a return statement if it is inside a
9873 built-in array notation reduction function. */
9874 if (!find_rank (loc, retval, retval, false, &rank))
9875 return error_mark_node;
9876 if (rank >= 1)
9877 {
9878 error_at (loc, "array notation expression cannot be used as a "
9879 "return value");
9880 return error_mark_node;
9881 }
9882 }
9883 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9884 {
9885 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9886 "allowed");
9887 return error_mark_node;
9888 }
9889 if (retval)
9890 {
9891 tree semantic_type = NULL_TREE;
9892 npc = null_pointer_constant_p (retval);
9893 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9894 {
9895 semantic_type = TREE_TYPE (retval);
9896 retval = TREE_OPERAND (retval, 0);
9897 }
9898 retval = c_fully_fold (retval, false, NULL);
9899 if (semantic_type)
9900 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9901 }
9902
9903 if (!retval)
9904 {
9905 current_function_returns_null = 1;
9906 if ((warn_return_type || flag_isoc99)
9907 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
9908 {
9909 bool warned_here;
9910 if (flag_isoc99)
9911 warned_here = pedwarn
9912 (loc, 0,
9913 "%<return%> with no value, in function returning non-void");
9914 else
9915 warned_here = warning_at
9916 (loc, OPT_Wreturn_type,
9917 "%<return%> with no value, in function returning non-void");
9918 no_warning = true;
9919 if (warned_here)
9920 inform (DECL_SOURCE_LOCATION (current_function_decl),
9921 "declared here");
9922 }
9923 }
9924 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
9925 {
9926 current_function_returns_null = 1;
9927 bool warned_here;
9928 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9929 warned_here = pedwarn
9930 (xloc, 0,
9931 "%<return%> with a value, in function returning void");
9932 else
9933 warned_here = pedwarn
9934 (xloc, OPT_Wpedantic, "ISO C forbids "
9935 "%<return%> with expression, in function returning void");
9936 if (warned_here)
9937 inform (DECL_SOURCE_LOCATION (current_function_decl),
9938 "declared here");
9939 }
9940 else
9941 {
9942 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9943 retval, origtype, ic_return,
9944 npc, NULL_TREE, NULL_TREE, 0);
9945 tree res = DECL_RESULT (current_function_decl);
9946 tree inner;
9947 bool save;
9948
9949 current_function_returns_value = 1;
9950 if (t == error_mark_node)
9951 return NULL_TREE;
9952
9953 save = in_late_binary_op;
9954 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9955 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9956 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9957 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9958 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9959 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
9960 in_late_binary_op = true;
9961 inner = t = convert (TREE_TYPE (res), t);
9962 in_late_binary_op = save;
9963
9964 /* Strip any conversions, additions, and subtractions, and see if
9965 we are returning the address of a local variable. Warn if so. */
9966 while (1)
9967 {
9968 switch (TREE_CODE (inner))
9969 {
9970 CASE_CONVERT:
9971 case NON_LVALUE_EXPR:
9972 case PLUS_EXPR:
9973 case POINTER_PLUS_EXPR:
9974 inner = TREE_OPERAND (inner, 0);
9975 continue;
9976
9977 case MINUS_EXPR:
9978 /* If the second operand of the MINUS_EXPR has a pointer
9979 type (or is converted from it), this may be valid, so
9980 don't give a warning. */
9981 {
9982 tree op1 = TREE_OPERAND (inner, 1);
9983
9984 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9985 && (CONVERT_EXPR_P (op1)
9986 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9987 op1 = TREE_OPERAND (op1, 0);
9988
9989 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9990 break;
9991
9992 inner = TREE_OPERAND (inner, 0);
9993 continue;
9994 }
9995
9996 case ADDR_EXPR:
9997 inner = TREE_OPERAND (inner, 0);
9998
9999 while (REFERENCE_CLASS_P (inner)
10000 && !INDIRECT_REF_P (inner))
10001 inner = TREE_OPERAND (inner, 0);
10002
10003 if (DECL_P (inner)
10004 && !DECL_EXTERNAL (inner)
10005 && !TREE_STATIC (inner)
10006 && DECL_CONTEXT (inner) == current_function_decl)
10007 {
10008 if (TREE_CODE (inner) == LABEL_DECL)
10009 warning_at (loc, OPT_Wreturn_local_addr,
10010 "function returns address of label");
10011 else
10012 {
10013 warning_at (loc, OPT_Wreturn_local_addr,
10014 "function returns address of local variable");
10015 tree zero = build_zero_cst (TREE_TYPE (res));
10016 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10017 }
10018 }
10019 break;
10020
10021 default:
10022 break;
10023 }
10024
10025 break;
10026 }
10027
10028 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10029 SET_EXPR_LOCATION (retval, loc);
10030
10031 if (warn_sequence_point)
10032 verify_sequence_points (retval);
10033 }
10034
10035 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10036 TREE_NO_WARNING (ret_stmt) |= no_warning;
10037 return add_stmt (ret_stmt);
10038 }
10039 \f
10040 struct c_switch {
10041 /* The SWITCH_EXPR being built. */
10042 tree switch_expr;
10043
10044 /* The original type of the testing expression, i.e. before the
10045 default conversion is applied. */
10046 tree orig_type;
10047
10048 /* A splay-tree mapping the low element of a case range to the high
10049 element, or NULL_TREE if there is no high element. Used to
10050 determine whether or not a new case label duplicates an old case
10051 label. We need a tree, rather than simply a hash table, because
10052 of the GNU case range extension. */
10053 splay_tree cases;
10054
10055 /* The bindings at the point of the switch. This is used for
10056 warnings crossing decls when branching to a case label. */
10057 struct c_spot_bindings *bindings;
10058
10059 /* The next node on the stack. */
10060 struct c_switch *next;
10061
10062 /* Remember whether the controlling expression had boolean type
10063 before integer promotions for the sake of -Wswitch-bool. */
10064 bool bool_cond_p;
10065
10066 /* Remember whether there was a case value that is outside the
10067 range of the ORIG_TYPE. */
10068 bool outside_range_p;
10069 };
10070
10071 /* A stack of the currently active switch statements. The innermost
10072 switch statement is on the top of the stack. There is no need to
10073 mark the stack for garbage collection because it is only active
10074 during the processing of the body of a function, and we never
10075 collect at that point. */
10076
10077 struct c_switch *c_switch_stack;
10078
10079 /* Start a C switch statement, testing expression EXP. Return the new
10080 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10081 SWITCH_COND_LOC is the location of the switch's condition.
10082 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10083
10084 tree
10085 c_start_case (location_t switch_loc,
10086 location_t switch_cond_loc,
10087 tree exp, bool explicit_cast_p)
10088 {
10089 tree orig_type = error_mark_node;
10090 bool bool_cond_p = false;
10091 struct c_switch *cs;
10092
10093 if (exp != error_mark_node)
10094 {
10095 orig_type = TREE_TYPE (exp);
10096
10097 if (!INTEGRAL_TYPE_P (orig_type))
10098 {
10099 if (orig_type != error_mark_node)
10100 {
10101 error_at (switch_cond_loc, "switch quantity not an integer");
10102 orig_type = error_mark_node;
10103 }
10104 exp = integer_zero_node;
10105 }
10106 else
10107 {
10108 tree type = TYPE_MAIN_VARIANT (orig_type);
10109 tree e = exp;
10110
10111 /* Warn if the condition has boolean value. */
10112 while (TREE_CODE (e) == COMPOUND_EXPR)
10113 e = TREE_OPERAND (e, 1);
10114
10115 if ((TREE_CODE (type) == BOOLEAN_TYPE
10116 || truth_value_p (TREE_CODE (e)))
10117 /* Explicit cast to int suppresses this warning. */
10118 && !(TREE_CODE (type) == INTEGER_TYPE
10119 && explicit_cast_p))
10120 bool_cond_p = true;
10121
10122 if (!in_system_header_at (input_location)
10123 && (type == long_integer_type_node
10124 || type == long_unsigned_type_node))
10125 warning_at (switch_cond_loc,
10126 OPT_Wtraditional, "%<long%> switch expression not "
10127 "converted to %<int%> in ISO C");
10128
10129 exp = c_fully_fold (exp, false, NULL);
10130 exp = default_conversion (exp);
10131
10132 if (warn_sequence_point)
10133 verify_sequence_points (exp);
10134 }
10135 }
10136
10137 /* Add this new SWITCH_EXPR to the stack. */
10138 cs = XNEW (struct c_switch);
10139 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
10140 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10141 cs->orig_type = orig_type;
10142 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10143 cs->bindings = c_get_switch_bindings ();
10144 cs->bool_cond_p = bool_cond_p;
10145 cs->outside_range_p = false;
10146 cs->next = c_switch_stack;
10147 c_switch_stack = cs;
10148
10149 return add_stmt (cs->switch_expr);
10150 }
10151
10152 /* Process a case label at location LOC. */
10153
10154 tree
10155 do_case (location_t loc, tree low_value, tree high_value)
10156 {
10157 tree label = NULL_TREE;
10158
10159 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10160 {
10161 low_value = c_fully_fold (low_value, false, NULL);
10162 if (TREE_CODE (low_value) == INTEGER_CST)
10163 pedwarn (loc, OPT_Wpedantic,
10164 "case label is not an integer constant expression");
10165 }
10166
10167 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10168 {
10169 high_value = c_fully_fold (high_value, false, NULL);
10170 if (TREE_CODE (high_value) == INTEGER_CST)
10171 pedwarn (input_location, OPT_Wpedantic,
10172 "case label is not an integer constant expression");
10173 }
10174
10175 if (c_switch_stack == NULL)
10176 {
10177 if (low_value)
10178 error_at (loc, "case label not within a switch statement");
10179 else
10180 error_at (loc, "%<default%> label not within a switch statement");
10181 return NULL_TREE;
10182 }
10183
10184 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10185 EXPR_LOCATION (c_switch_stack->switch_expr),
10186 loc))
10187 return NULL_TREE;
10188
10189 label = c_add_case_label (loc, c_switch_stack->cases,
10190 SWITCH_COND (c_switch_stack->switch_expr),
10191 c_switch_stack->orig_type,
10192 low_value, high_value,
10193 &c_switch_stack->outside_range_p);
10194 if (label == error_mark_node)
10195 label = NULL_TREE;
10196 return label;
10197 }
10198
10199 /* Finish the switch statement. TYPE is the original type of the
10200 controlling expression of the switch, or NULL_TREE. */
10201
10202 void
10203 c_finish_case (tree body, tree type)
10204 {
10205 struct c_switch *cs = c_switch_stack;
10206 location_t switch_location;
10207
10208 SWITCH_BODY (cs->switch_expr) = body;
10209
10210 /* Emit warnings as needed. */
10211 switch_location = EXPR_LOCATION (cs->switch_expr);
10212 c_do_switch_warnings (cs->cases, switch_location,
10213 type ? type : TREE_TYPE (cs->switch_expr),
10214 SWITCH_COND (cs->switch_expr),
10215 cs->bool_cond_p, cs->outside_range_p);
10216
10217 /* Pop the stack. */
10218 c_switch_stack = cs->next;
10219 splay_tree_delete (cs->cases);
10220 c_release_switch_bindings (cs->bindings);
10221 XDELETE (cs);
10222 }
10223 \f
10224 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10225 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10226 may be null. */
10227
10228 void
10229 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10230 tree else_block)
10231 {
10232 tree stmt;
10233
10234 /* If the condition has array notations, then the rank of the then_block and
10235 else_block must be either 0 or be equal to the rank of the condition. If
10236 the condition does not have array notations then break them up as it is
10237 broken up in a normal expression. */
10238 if (flag_cilkplus && contains_array_notation_expr (cond))
10239 {
10240 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10241 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10242 return;
10243 if (then_block
10244 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10245 return;
10246 if (else_block
10247 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10248 return;
10249 if (cond_rank != then_rank && then_rank != 0)
10250 {
10251 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10252 " and the then-block");
10253 return;
10254 }
10255 else if (cond_rank != else_rank && else_rank != 0)
10256 {
10257 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10258 " and the else-block");
10259 return;
10260 }
10261 }
10262
10263 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10264 SET_EXPR_LOCATION (stmt, if_locus);
10265 add_stmt (stmt);
10266 }
10267
10268 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10269 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10270 is false for DO loops. INCR is the FOR increment expression. BODY is
10271 the statement controlled by the loop. BLAB is the break label. CLAB is
10272 the continue label. Everything is allowed to be NULL. */
10273
10274 void
10275 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10276 tree blab, tree clab, bool cond_is_first)
10277 {
10278 tree entry = NULL, exit = NULL, t;
10279
10280 /* In theory could forbid cilk spawn for loop increment expression,
10281 but it should work just fine. */
10282
10283 /* If the condition is zero don't generate a loop construct. */
10284 if (cond && integer_zerop (cond))
10285 {
10286 if (cond_is_first)
10287 {
10288 t = build_and_jump (&blab);
10289 SET_EXPR_LOCATION (t, start_locus);
10290 add_stmt (t);
10291 }
10292 }
10293 else
10294 {
10295 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10296
10297 /* If we have an exit condition, then we build an IF with gotos either
10298 out of the loop, or to the top of it. If there's no exit condition,
10299 then we just build a jump back to the top. */
10300 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10301
10302 if (cond && !integer_nonzerop (cond))
10303 {
10304 /* Canonicalize the loop condition to the end. This means
10305 generating a branch to the loop condition. Reuse the
10306 continue label, if possible. */
10307 if (cond_is_first)
10308 {
10309 if (incr || !clab)
10310 {
10311 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10312 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10313 }
10314 else
10315 t = build1 (GOTO_EXPR, void_type_node, clab);
10316 SET_EXPR_LOCATION (t, start_locus);
10317 add_stmt (t);
10318 }
10319
10320 t = build_and_jump (&blab);
10321 if (cond_is_first)
10322 exit = fold_build3_loc (start_locus,
10323 COND_EXPR, void_type_node, cond, exit, t);
10324 else
10325 exit = fold_build3_loc (input_location,
10326 COND_EXPR, void_type_node, cond, exit, t);
10327 }
10328 else
10329 {
10330 /* For the backward-goto's location of an unconditional loop
10331 use the beginning of the body, or, if there is none, the
10332 top of the loop. */
10333 location_t loc = EXPR_LOCATION (expr_first (body));
10334 if (loc == UNKNOWN_LOCATION)
10335 loc = start_locus;
10336 SET_EXPR_LOCATION (exit, loc);
10337 }
10338
10339 add_stmt (top);
10340 }
10341
10342 if (body)
10343 add_stmt (body);
10344 if (clab)
10345 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10346 if (incr)
10347 add_stmt (incr);
10348 if (entry)
10349 add_stmt (entry);
10350 if (exit)
10351 add_stmt (exit);
10352 if (blab)
10353 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10354 }
10355
10356 tree
10357 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10358 {
10359 bool skip;
10360 tree label = *label_p;
10361
10362 /* In switch statements break is sometimes stylistically used after
10363 a return statement. This can lead to spurious warnings about
10364 control reaching the end of a non-void function when it is
10365 inlined. Note that we are calling block_may_fallthru with
10366 language specific tree nodes; this works because
10367 block_may_fallthru returns true when given something it does not
10368 understand. */
10369 skip = !block_may_fallthru (cur_stmt_list);
10370
10371 if (!label)
10372 {
10373 if (!skip)
10374 *label_p = label = create_artificial_label (loc);
10375 }
10376 else if (TREE_CODE (label) == LABEL_DECL)
10377 ;
10378 else switch (TREE_INT_CST_LOW (label))
10379 {
10380 case 0:
10381 if (is_break)
10382 error_at (loc, "break statement not within loop or switch");
10383 else
10384 error_at (loc, "continue statement not within a loop");
10385 return NULL_TREE;
10386
10387 case 1:
10388 gcc_assert (is_break);
10389 error_at (loc, "break statement used with OpenMP for loop");
10390 return NULL_TREE;
10391
10392 case 2:
10393 if (is_break)
10394 error ("break statement within %<#pragma simd%> loop body");
10395 else
10396 error ("continue statement within %<#pragma simd%> loop body");
10397 return NULL_TREE;
10398
10399 default:
10400 gcc_unreachable ();
10401 }
10402
10403 if (skip)
10404 return NULL_TREE;
10405
10406 if (!is_break)
10407 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10408
10409 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10410 }
10411
10412 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10413
10414 static void
10415 emit_side_effect_warnings (location_t loc, tree expr)
10416 {
10417 if (expr == error_mark_node)
10418 ;
10419 else if (!TREE_SIDE_EFFECTS (expr))
10420 {
10421 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10422 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10423 }
10424 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10425 {
10426 tree r = expr;
10427 location_t cloc = loc;
10428 while (TREE_CODE (r) == COMPOUND_EXPR)
10429 {
10430 if (EXPR_HAS_LOCATION (r))
10431 cloc = EXPR_LOCATION (r);
10432 r = TREE_OPERAND (r, 1);
10433 }
10434 if (!TREE_SIDE_EFFECTS (r)
10435 && !VOID_TYPE_P (TREE_TYPE (r))
10436 && !CONVERT_EXPR_P (r)
10437 && !TREE_NO_WARNING (r)
10438 && !TREE_NO_WARNING (expr))
10439 warning_at (cloc, OPT_Wunused_value,
10440 "right-hand operand of comma expression has no effect");
10441 }
10442 else
10443 warn_if_unused_value (expr, loc);
10444 }
10445
10446 /* Process an expression as if it were a complete statement. Emit
10447 diagnostics, but do not call ADD_STMT. LOC is the location of the
10448 statement. */
10449
10450 tree
10451 c_process_expr_stmt (location_t loc, tree expr)
10452 {
10453 tree exprv;
10454
10455 if (!expr)
10456 return NULL_TREE;
10457
10458 expr = c_fully_fold (expr, false, NULL);
10459
10460 if (warn_sequence_point)
10461 verify_sequence_points (expr);
10462
10463 if (TREE_TYPE (expr) != error_mark_node
10464 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10465 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10466 error_at (loc, "expression statement has incomplete type");
10467
10468 /* If we're not processing a statement expression, warn about unused values.
10469 Warnings for statement expressions will be emitted later, once we figure
10470 out which is the result. */
10471 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10472 && warn_unused_value)
10473 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10474
10475 exprv = expr;
10476 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10477 exprv = TREE_OPERAND (exprv, 1);
10478 while (CONVERT_EXPR_P (exprv))
10479 exprv = TREE_OPERAND (exprv, 0);
10480 if (DECL_P (exprv)
10481 || handled_component_p (exprv)
10482 || TREE_CODE (exprv) == ADDR_EXPR)
10483 mark_exp_read (exprv);
10484
10485 /* If the expression is not of a type to which we cannot assign a line
10486 number, wrap the thing in a no-op NOP_EXPR. */
10487 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10488 {
10489 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10490 SET_EXPR_LOCATION (expr, loc);
10491 }
10492
10493 return expr;
10494 }
10495
10496 /* Emit an expression as a statement. LOC is the location of the
10497 expression. */
10498
10499 tree
10500 c_finish_expr_stmt (location_t loc, tree expr)
10501 {
10502 if (expr)
10503 return add_stmt (c_process_expr_stmt (loc, expr));
10504 else
10505 return NULL;
10506 }
10507
10508 /* Do the opposite and emit a statement as an expression. To begin,
10509 create a new binding level and return it. */
10510
10511 tree
10512 c_begin_stmt_expr (void)
10513 {
10514 tree ret;
10515
10516 /* We must force a BLOCK for this level so that, if it is not expanded
10517 later, there is a way to turn off the entire subtree of blocks that
10518 are contained in it. */
10519 keep_next_level ();
10520 ret = c_begin_compound_stmt (true);
10521
10522 c_bindings_start_stmt_expr (c_switch_stack == NULL
10523 ? NULL
10524 : c_switch_stack->bindings);
10525
10526 /* Mark the current statement list as belonging to a statement list. */
10527 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10528
10529 return ret;
10530 }
10531
10532 /* LOC is the location of the compound statement to which this body
10533 belongs. */
10534
10535 tree
10536 c_finish_stmt_expr (location_t loc, tree body)
10537 {
10538 tree last, type, tmp, val;
10539 tree *last_p;
10540
10541 body = c_end_compound_stmt (loc, body, true);
10542
10543 c_bindings_end_stmt_expr (c_switch_stack == NULL
10544 ? NULL
10545 : c_switch_stack->bindings);
10546
10547 /* Locate the last statement in BODY. See c_end_compound_stmt
10548 about always returning a BIND_EXPR. */
10549 last_p = &BIND_EXPR_BODY (body);
10550 last = BIND_EXPR_BODY (body);
10551
10552 continue_searching:
10553 if (TREE_CODE (last) == STATEMENT_LIST)
10554 {
10555 tree_stmt_iterator i;
10556
10557 /* This can happen with degenerate cases like ({ }). No value. */
10558 if (!TREE_SIDE_EFFECTS (last))
10559 return body;
10560
10561 /* If we're supposed to generate side effects warnings, process
10562 all of the statements except the last. */
10563 if (warn_unused_value)
10564 {
10565 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10566 {
10567 location_t tloc;
10568 tree t = tsi_stmt (i);
10569
10570 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10571 emit_side_effect_warnings (tloc, t);
10572 }
10573 }
10574 else
10575 i = tsi_last (last);
10576 last_p = tsi_stmt_ptr (i);
10577 last = *last_p;
10578 }
10579
10580 /* If the end of the list is exception related, then the list was split
10581 by a call to push_cleanup. Continue searching. */
10582 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10583 || TREE_CODE (last) == TRY_CATCH_EXPR)
10584 {
10585 last_p = &TREE_OPERAND (last, 0);
10586 last = *last_p;
10587 goto continue_searching;
10588 }
10589
10590 if (last == error_mark_node)
10591 return last;
10592
10593 /* In the case that the BIND_EXPR is not necessary, return the
10594 expression out from inside it. */
10595 if (last == BIND_EXPR_BODY (body)
10596 && BIND_EXPR_VARS (body) == NULL)
10597 {
10598 /* Even if this looks constant, do not allow it in a constant
10599 expression. */
10600 last = c_wrap_maybe_const (last, true);
10601 /* Do not warn if the return value of a statement expression is
10602 unused. */
10603 TREE_NO_WARNING (last) = 1;
10604 return last;
10605 }
10606
10607 /* Extract the type of said expression. */
10608 type = TREE_TYPE (last);
10609
10610 /* If we're not returning a value at all, then the BIND_EXPR that
10611 we already have is a fine expression to return. */
10612 if (!type || VOID_TYPE_P (type))
10613 return body;
10614
10615 /* Now that we've located the expression containing the value, it seems
10616 silly to make voidify_wrapper_expr repeat the process. Create a
10617 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10618 tmp = create_tmp_var_raw (type);
10619
10620 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10621 tree_expr_nonnegative_p giving up immediately. */
10622 val = last;
10623 if (TREE_CODE (val) == NOP_EXPR
10624 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10625 val = TREE_OPERAND (val, 0);
10626
10627 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10628 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10629
10630 {
10631 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10632 SET_EXPR_LOCATION (t, loc);
10633 return t;
10634 }
10635 }
10636 \f
10637 /* Begin and end compound statements. This is as simple as pushing
10638 and popping new statement lists from the tree. */
10639
10640 tree
10641 c_begin_compound_stmt (bool do_scope)
10642 {
10643 tree stmt = push_stmt_list ();
10644 if (do_scope)
10645 push_scope ();
10646 return stmt;
10647 }
10648
10649 /* End a compound statement. STMT is the statement. LOC is the
10650 location of the compound statement-- this is usually the location
10651 of the opening brace. */
10652
10653 tree
10654 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10655 {
10656 tree block = NULL;
10657
10658 if (do_scope)
10659 {
10660 if (c_dialect_objc ())
10661 objc_clear_super_receiver ();
10662 block = pop_scope ();
10663 }
10664
10665 stmt = pop_stmt_list (stmt);
10666 stmt = c_build_bind_expr (loc, block, stmt);
10667
10668 /* If this compound statement is nested immediately inside a statement
10669 expression, then force a BIND_EXPR to be created. Otherwise we'll
10670 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10671 STATEMENT_LISTs merge, and thus we can lose track of what statement
10672 was really last. */
10673 if (building_stmt_list_p ()
10674 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10675 && TREE_CODE (stmt) != BIND_EXPR)
10676 {
10677 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10678 TREE_SIDE_EFFECTS (stmt) = 1;
10679 SET_EXPR_LOCATION (stmt, loc);
10680 }
10681
10682 return stmt;
10683 }
10684
10685 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10686 when the current scope is exited. EH_ONLY is true when this is not
10687 meant to apply to normal control flow transfer. */
10688
10689 void
10690 push_cleanup (tree decl, tree cleanup, bool eh_only)
10691 {
10692 enum tree_code code;
10693 tree stmt, list;
10694 bool stmt_expr;
10695
10696 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10697 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10698 add_stmt (stmt);
10699 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10700 list = push_stmt_list ();
10701 TREE_OPERAND (stmt, 0) = list;
10702 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10703 }
10704 \f
10705 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10706 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10707
10708 static tree
10709 build_vec_cmp (tree_code code, tree type,
10710 tree arg0, tree arg1)
10711 {
10712 tree zero_vec = build_zero_cst (type);
10713 tree minus_one_vec = build_minus_one_cst (type);
10714 tree cmp_type = build_same_sized_truth_vector_type (type);
10715 tree cmp = build2 (code, cmp_type, arg0, arg1);
10716 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10717 }
10718
10719 /* Build a binary-operation expression without default conversions.
10720 CODE is the kind of expression to build.
10721 LOCATION is the operator's location.
10722 This function differs from `build' in several ways:
10723 the data type of the result is computed and recorded in it,
10724 warnings are generated if arg data types are invalid,
10725 special handling for addition and subtraction of pointers is known,
10726 and some optimization is done (operations on narrow ints
10727 are done in the narrower type when that gives the same result).
10728 Constant folding is also done before the result is returned.
10729
10730 Note that the operands will never have enumeral types, or function
10731 or array types, because either they will have the default conversions
10732 performed or they have both just been converted to some other type in which
10733 the arithmetic is to be done. */
10734
10735 tree
10736 build_binary_op (location_t location, enum tree_code code,
10737 tree orig_op0, tree orig_op1, int convert_p)
10738 {
10739 tree type0, type1, orig_type0, orig_type1;
10740 tree eptype;
10741 enum tree_code code0, code1;
10742 tree op0, op1;
10743 tree ret = error_mark_node;
10744 const char *invalid_op_diag;
10745 bool op0_int_operands, op1_int_operands;
10746 bool int_const, int_const_or_overflow, int_operands;
10747
10748 /* Expression code to give to the expression when it is built.
10749 Normally this is CODE, which is what the caller asked for,
10750 but in some special cases we change it. */
10751 enum tree_code resultcode = code;
10752
10753 /* Data type in which the computation is to be performed.
10754 In the simplest cases this is the common type of the arguments. */
10755 tree result_type = NULL;
10756
10757 /* When the computation is in excess precision, the type of the
10758 final EXCESS_PRECISION_EXPR. */
10759 tree semantic_result_type = NULL;
10760
10761 /* Nonzero means operands have already been type-converted
10762 in whatever way is necessary.
10763 Zero means they need to be converted to RESULT_TYPE. */
10764 int converted = 0;
10765
10766 /* Nonzero means create the expression with this type, rather than
10767 RESULT_TYPE. */
10768 tree build_type = NULL_TREE;
10769
10770 /* Nonzero means after finally constructing the expression
10771 convert it to this type. */
10772 tree final_type = NULL_TREE;
10773
10774 /* Nonzero if this is an operation like MIN or MAX which can
10775 safely be computed in short if both args are promoted shorts.
10776 Also implies COMMON.
10777 -1 indicates a bitwise operation; this makes a difference
10778 in the exact conditions for when it is safe to do the operation
10779 in a narrower mode. */
10780 int shorten = 0;
10781
10782 /* Nonzero if this is a comparison operation;
10783 if both args are promoted shorts, compare the original shorts.
10784 Also implies COMMON. */
10785 int short_compare = 0;
10786
10787 /* Nonzero if this is a right-shift operation, which can be computed on the
10788 original short and then promoted if the operand is a promoted short. */
10789 int short_shift = 0;
10790
10791 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10792 int common = 0;
10793
10794 /* True means types are compatible as far as ObjC is concerned. */
10795 bool objc_ok;
10796
10797 /* True means this is an arithmetic operation that may need excess
10798 precision. */
10799 bool may_need_excess_precision;
10800
10801 /* True means this is a boolean operation that converts both its
10802 operands to truth-values. */
10803 bool boolean_op = false;
10804
10805 /* Remember whether we're doing / or %. */
10806 bool doing_div_or_mod = false;
10807
10808 /* Remember whether we're doing << or >>. */
10809 bool doing_shift = false;
10810
10811 /* Tree holding instrumentation expression. */
10812 tree instrument_expr = NULL;
10813
10814 if (location == UNKNOWN_LOCATION)
10815 location = input_location;
10816
10817 op0 = orig_op0;
10818 op1 = orig_op1;
10819
10820 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10821 if (op0_int_operands)
10822 op0 = remove_c_maybe_const_expr (op0);
10823 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10824 if (op1_int_operands)
10825 op1 = remove_c_maybe_const_expr (op1);
10826 int_operands = (op0_int_operands && op1_int_operands);
10827 if (int_operands)
10828 {
10829 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10830 && TREE_CODE (orig_op1) == INTEGER_CST);
10831 int_const = (int_const_or_overflow
10832 && !TREE_OVERFLOW (orig_op0)
10833 && !TREE_OVERFLOW (orig_op1));
10834 }
10835 else
10836 int_const = int_const_or_overflow = false;
10837
10838 /* Do not apply default conversion in mixed vector/scalar expression. */
10839 if (convert_p
10840 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
10841 {
10842 op0 = default_conversion (op0);
10843 op1 = default_conversion (op1);
10844 }
10845
10846 /* When Cilk Plus is enabled and there are array notations inside op0, then
10847 we check to see if there are builtin array notation functions. If
10848 so, then we take on the type of the array notation inside it. */
10849 if (flag_cilkplus && contains_array_notation_expr (op0))
10850 orig_type0 = type0 = find_correct_array_notation_type (op0);
10851 else
10852 orig_type0 = type0 = TREE_TYPE (op0);
10853
10854 if (flag_cilkplus && contains_array_notation_expr (op1))
10855 orig_type1 = type1 = find_correct_array_notation_type (op1);
10856 else
10857 orig_type1 = type1 = TREE_TYPE (op1);
10858
10859 /* The expression codes of the data types of the arguments tell us
10860 whether the arguments are integers, floating, pointers, etc. */
10861 code0 = TREE_CODE (type0);
10862 code1 = TREE_CODE (type1);
10863
10864 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10865 STRIP_TYPE_NOPS (op0);
10866 STRIP_TYPE_NOPS (op1);
10867
10868 /* If an error was already reported for one of the arguments,
10869 avoid reporting another error. */
10870
10871 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10872 return error_mark_node;
10873
10874 if (code0 == POINTER_TYPE
10875 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10876 return error_mark_node;
10877
10878 if (code1 == POINTER_TYPE
10879 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10880 return error_mark_node;
10881
10882 if ((invalid_op_diag
10883 = targetm.invalid_binary_op (code, type0, type1)))
10884 {
10885 error_at (location, invalid_op_diag);
10886 return error_mark_node;
10887 }
10888
10889 switch (code)
10890 {
10891 case PLUS_EXPR:
10892 case MINUS_EXPR:
10893 case MULT_EXPR:
10894 case TRUNC_DIV_EXPR:
10895 case CEIL_DIV_EXPR:
10896 case FLOOR_DIV_EXPR:
10897 case ROUND_DIV_EXPR:
10898 case EXACT_DIV_EXPR:
10899 may_need_excess_precision = true;
10900 break;
10901 default:
10902 may_need_excess_precision = false;
10903 break;
10904 }
10905 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10906 {
10907 op0 = TREE_OPERAND (op0, 0);
10908 type0 = TREE_TYPE (op0);
10909 }
10910 else if (may_need_excess_precision
10911 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10912 {
10913 type0 = eptype;
10914 op0 = convert (eptype, op0);
10915 }
10916 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10917 {
10918 op1 = TREE_OPERAND (op1, 0);
10919 type1 = TREE_TYPE (op1);
10920 }
10921 else if (may_need_excess_precision
10922 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10923 {
10924 type1 = eptype;
10925 op1 = convert (eptype, op1);
10926 }
10927
10928 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10929
10930 /* In case when one of the operands of the binary operation is
10931 a vector and another is a scalar -- convert scalar to vector. */
10932 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10933 {
10934 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10935 true);
10936
10937 switch (convert_flag)
10938 {
10939 case stv_error:
10940 return error_mark_node;
10941 case stv_firstarg:
10942 {
10943 bool maybe_const = true;
10944 tree sc;
10945 sc = c_fully_fold (op0, false, &maybe_const);
10946 sc = save_expr (sc);
10947 sc = convert (TREE_TYPE (type1), sc);
10948 op0 = build_vector_from_val (type1, sc);
10949 if (!maybe_const)
10950 op0 = c_wrap_maybe_const (op0, true);
10951 orig_type0 = type0 = TREE_TYPE (op0);
10952 code0 = TREE_CODE (type0);
10953 converted = 1;
10954 break;
10955 }
10956 case stv_secondarg:
10957 {
10958 bool maybe_const = true;
10959 tree sc;
10960 sc = c_fully_fold (op1, false, &maybe_const);
10961 sc = save_expr (sc);
10962 sc = convert (TREE_TYPE (type0), sc);
10963 op1 = build_vector_from_val (type0, sc);
10964 if (!maybe_const)
10965 op1 = c_wrap_maybe_const (op1, true);
10966 orig_type1 = type1 = TREE_TYPE (op1);
10967 code1 = TREE_CODE (type1);
10968 converted = 1;
10969 break;
10970 }
10971 default:
10972 break;
10973 }
10974 }
10975
10976 switch (code)
10977 {
10978 case PLUS_EXPR:
10979 /* Handle the pointer + int case. */
10980 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10981 {
10982 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10983 goto return_build_binary_op;
10984 }
10985 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10986 {
10987 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10988 goto return_build_binary_op;
10989 }
10990 else
10991 common = 1;
10992 break;
10993
10994 case MINUS_EXPR:
10995 /* Subtraction of two similar pointers.
10996 We must subtract them as integers, then divide by object size. */
10997 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10998 && comp_target_types (location, type0, type1))
10999 {
11000 ret = pointer_diff (location, op0, op1);
11001 goto return_build_binary_op;
11002 }
11003 /* Handle pointer minus int. Just like pointer plus int. */
11004 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11005 {
11006 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11007 goto return_build_binary_op;
11008 }
11009 else
11010 common = 1;
11011 break;
11012
11013 case MULT_EXPR:
11014 common = 1;
11015 break;
11016
11017 case TRUNC_DIV_EXPR:
11018 case CEIL_DIV_EXPR:
11019 case FLOOR_DIV_EXPR:
11020 case ROUND_DIV_EXPR:
11021 case EXACT_DIV_EXPR:
11022 doing_div_or_mod = true;
11023 warn_for_div_by_zero (location, op1);
11024
11025 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11026 || code0 == FIXED_POINT_TYPE
11027 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11028 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11029 || code1 == FIXED_POINT_TYPE
11030 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
11031 {
11032 enum tree_code tcode0 = code0, tcode1 = code1;
11033
11034 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11035 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11036 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11037 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11038
11039 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11040 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11041 resultcode = RDIV_EXPR;
11042 else
11043 /* Although it would be tempting to shorten always here, that
11044 loses on some targets, since the modulo instruction is
11045 undefined if the quotient can't be represented in the
11046 computation mode. We shorten only if unsigned or if
11047 dividing by something we know != -1. */
11048 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11049 || (TREE_CODE (op1) == INTEGER_CST
11050 && !integer_all_onesp (op1)));
11051 common = 1;
11052 }
11053 break;
11054
11055 case BIT_AND_EXPR:
11056 case BIT_IOR_EXPR:
11057 case BIT_XOR_EXPR:
11058 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11059 shorten = -1;
11060 /* Allow vector types which are not floating point types. */
11061 else if (code0 == VECTOR_TYPE
11062 && code1 == VECTOR_TYPE
11063 && !VECTOR_FLOAT_TYPE_P (type0)
11064 && !VECTOR_FLOAT_TYPE_P (type1))
11065 common = 1;
11066 break;
11067
11068 case TRUNC_MOD_EXPR:
11069 case FLOOR_MOD_EXPR:
11070 doing_div_or_mod = true;
11071 warn_for_div_by_zero (location, op1);
11072
11073 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11074 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11075 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11076 common = 1;
11077 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11078 {
11079 /* Although it would be tempting to shorten always here, that loses
11080 on some targets, since the modulo instruction is undefined if the
11081 quotient can't be represented in the computation mode. We shorten
11082 only if unsigned or if dividing by something we know != -1. */
11083 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11084 || (TREE_CODE (op1) == INTEGER_CST
11085 && !integer_all_onesp (op1)));
11086 common = 1;
11087 }
11088 break;
11089
11090 case TRUTH_ANDIF_EXPR:
11091 case TRUTH_ORIF_EXPR:
11092 case TRUTH_AND_EXPR:
11093 case TRUTH_OR_EXPR:
11094 case TRUTH_XOR_EXPR:
11095 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11096 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11097 || code0 == FIXED_POINT_TYPE)
11098 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11099 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11100 || code1 == FIXED_POINT_TYPE))
11101 {
11102 /* Result of these operations is always an int,
11103 but that does not mean the operands should be
11104 converted to ints! */
11105 result_type = integer_type_node;
11106 if (op0_int_operands)
11107 {
11108 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11109 op0 = remove_c_maybe_const_expr (op0);
11110 }
11111 else
11112 op0 = c_objc_common_truthvalue_conversion (location, op0);
11113 if (op1_int_operands)
11114 {
11115 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11116 op1 = remove_c_maybe_const_expr (op1);
11117 }
11118 else
11119 op1 = c_objc_common_truthvalue_conversion (location, op1);
11120 converted = 1;
11121 boolean_op = true;
11122 }
11123 if (code == TRUTH_ANDIF_EXPR)
11124 {
11125 int_const_or_overflow = (int_operands
11126 && TREE_CODE (orig_op0) == INTEGER_CST
11127 && (op0 == truthvalue_false_node
11128 || TREE_CODE (orig_op1) == INTEGER_CST));
11129 int_const = (int_const_or_overflow
11130 && !TREE_OVERFLOW (orig_op0)
11131 && (op0 == truthvalue_false_node
11132 || !TREE_OVERFLOW (orig_op1)));
11133 }
11134 else if (code == TRUTH_ORIF_EXPR)
11135 {
11136 int_const_or_overflow = (int_operands
11137 && TREE_CODE (orig_op0) == INTEGER_CST
11138 && (op0 == truthvalue_true_node
11139 || TREE_CODE (orig_op1) == INTEGER_CST));
11140 int_const = (int_const_or_overflow
11141 && !TREE_OVERFLOW (orig_op0)
11142 && (op0 == truthvalue_true_node
11143 || !TREE_OVERFLOW (orig_op1)));
11144 }
11145 break;
11146
11147 /* Shift operations: result has same type as first operand;
11148 always convert second operand to int.
11149 Also set SHORT_SHIFT if shifting rightward. */
11150
11151 case RSHIFT_EXPR:
11152 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11153 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11154 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11155 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11156 {
11157 result_type = type0;
11158 converted = 1;
11159 }
11160 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11161 || code0 == VECTOR_TYPE)
11162 && code1 == INTEGER_TYPE)
11163 {
11164 doing_shift = true;
11165 if (TREE_CODE (op1) == INTEGER_CST)
11166 {
11167 if (tree_int_cst_sgn (op1) < 0)
11168 {
11169 int_const = false;
11170 if (c_inhibit_evaluation_warnings == 0)
11171 warning_at (location, OPT_Wshift_count_negative,
11172 "right shift count is negative");
11173 }
11174 else if (code0 == VECTOR_TYPE)
11175 {
11176 if (compare_tree_int (op1,
11177 TYPE_PRECISION (TREE_TYPE (type0)))
11178 >= 0)
11179 {
11180 int_const = false;
11181 if (c_inhibit_evaluation_warnings == 0)
11182 warning_at (location, OPT_Wshift_count_overflow,
11183 "right shift count >= width of vector element");
11184 }
11185 }
11186 else
11187 {
11188 if (!integer_zerop (op1))
11189 short_shift = 1;
11190
11191 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11192 {
11193 int_const = false;
11194 if (c_inhibit_evaluation_warnings == 0)
11195 warning_at (location, OPT_Wshift_count_overflow,
11196 "right shift count >= width of type");
11197 }
11198 }
11199 }
11200
11201 /* Use the type of the value to be shifted. */
11202 result_type = type0;
11203 /* Avoid converting op1 to result_type later. */
11204 converted = 1;
11205 }
11206 break;
11207
11208 case LSHIFT_EXPR:
11209 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11210 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11211 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11212 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11213 {
11214 result_type = type0;
11215 converted = 1;
11216 }
11217 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11218 || code0 == VECTOR_TYPE)
11219 && code1 == INTEGER_TYPE)
11220 {
11221 doing_shift = true;
11222 if (TREE_CODE (op0) == INTEGER_CST
11223 && tree_int_cst_sgn (op0) < 0)
11224 {
11225 /* Don't reject a left shift of a negative value in a context
11226 where a constant expression is needed in C90. */
11227 if (flag_isoc99)
11228 int_const = false;
11229 if (c_inhibit_evaluation_warnings == 0)
11230 warning_at (location, OPT_Wshift_negative_value,
11231 "left shift of negative value");
11232 }
11233 if (TREE_CODE (op1) == INTEGER_CST)
11234 {
11235 if (tree_int_cst_sgn (op1) < 0)
11236 {
11237 int_const = false;
11238 if (c_inhibit_evaluation_warnings == 0)
11239 warning_at (location, OPT_Wshift_count_negative,
11240 "left shift count is negative");
11241 }
11242 else if (code0 == VECTOR_TYPE)
11243 {
11244 if (compare_tree_int (op1,
11245 TYPE_PRECISION (TREE_TYPE (type0)))
11246 >= 0)
11247 {
11248 int_const = false;
11249 if (c_inhibit_evaluation_warnings == 0)
11250 warning_at (location, OPT_Wshift_count_overflow,
11251 "left shift count >= width of vector element");
11252 }
11253 }
11254 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11255 {
11256 int_const = false;
11257 if (c_inhibit_evaluation_warnings == 0)
11258 warning_at (location, OPT_Wshift_count_overflow,
11259 "left shift count >= width of type");
11260 }
11261 else if (TREE_CODE (op0) == INTEGER_CST
11262 && maybe_warn_shift_overflow (location, op0, op1)
11263 && flag_isoc99)
11264 int_const = false;
11265 }
11266
11267 /* Use the type of the value to be shifted. */
11268 result_type = type0;
11269 /* Avoid converting op1 to result_type later. */
11270 converted = 1;
11271 }
11272 break;
11273
11274 case EQ_EXPR:
11275 case NE_EXPR:
11276 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11277 {
11278 tree intt;
11279 if (!vector_types_compatible_elements_p (type0, type1))
11280 {
11281 error_at (location, "comparing vectors with different "
11282 "element types");
11283 return error_mark_node;
11284 }
11285
11286 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11287 {
11288 error_at (location, "comparing vectors with different "
11289 "number of elements");
11290 return error_mark_node;
11291 }
11292
11293 /* It's not precisely specified how the usual arithmetic
11294 conversions apply to the vector types. Here, we use
11295 the unsigned type if one of the operands is signed and
11296 the other one is unsigned. */
11297 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11298 {
11299 if (!TYPE_UNSIGNED (type0))
11300 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11301 else
11302 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11303 warning_at (location, OPT_Wsign_compare, "comparison between "
11304 "types %qT and %qT", type0, type1);
11305 }
11306
11307 /* Always construct signed integer vector type. */
11308 intt = c_common_type_for_size (GET_MODE_BITSIZE
11309 (TYPE_MODE (TREE_TYPE (type0))), 0);
11310 result_type = build_opaque_vector_type (intt,
11311 TYPE_VECTOR_SUBPARTS (type0));
11312 converted = 1;
11313 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11314 goto return_build_binary_op;
11315 }
11316 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11317 warning_at (location,
11318 OPT_Wfloat_equal,
11319 "comparing floating point with == or != is unsafe");
11320 /* Result of comparison is always int,
11321 but don't convert the args to int! */
11322 build_type = integer_type_node;
11323 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11324 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11325 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11326 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11327 short_compare = 1;
11328 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11329 {
11330 if (TREE_CODE (op0) == ADDR_EXPR
11331 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11332 && !from_macro_expansion_at (location))
11333 {
11334 if (code == EQ_EXPR)
11335 warning_at (location,
11336 OPT_Waddress,
11337 "the comparison will always evaluate as %<false%> "
11338 "for the address of %qD will never be NULL",
11339 TREE_OPERAND (op0, 0));
11340 else
11341 warning_at (location,
11342 OPT_Waddress,
11343 "the comparison will always evaluate as %<true%> "
11344 "for the address of %qD will never be NULL",
11345 TREE_OPERAND (op0, 0));
11346 }
11347 result_type = type0;
11348 }
11349 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11350 {
11351 if (TREE_CODE (op1) == ADDR_EXPR
11352 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11353 && !from_macro_expansion_at (location))
11354 {
11355 if (code == EQ_EXPR)
11356 warning_at (location,
11357 OPT_Waddress,
11358 "the comparison will always evaluate as %<false%> "
11359 "for the address of %qD will never be NULL",
11360 TREE_OPERAND (op1, 0));
11361 else
11362 warning_at (location,
11363 OPT_Waddress,
11364 "the comparison will always evaluate as %<true%> "
11365 "for the address of %qD will never be NULL",
11366 TREE_OPERAND (op1, 0));
11367 }
11368 result_type = type1;
11369 }
11370 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11371 {
11372 tree tt0 = TREE_TYPE (type0);
11373 tree tt1 = TREE_TYPE (type1);
11374 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11375 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11376 addr_space_t as_common = ADDR_SPACE_GENERIC;
11377
11378 /* Anything compares with void *. void * compares with anything.
11379 Otherwise, the targets must be compatible
11380 and both must be object or both incomplete. */
11381 if (comp_target_types (location, type0, type1))
11382 result_type = common_pointer_type (type0, type1);
11383 else if (!addr_space_superset (as0, as1, &as_common))
11384 {
11385 error_at (location, "comparison of pointers to "
11386 "disjoint address spaces");
11387 return error_mark_node;
11388 }
11389 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11390 {
11391 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11392 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11393 "comparison of %<void *%> with function pointer");
11394 }
11395 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11396 {
11397 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11398 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11399 "comparison of %<void *%> with function pointer");
11400 }
11401 else
11402 /* Avoid warning about the volatile ObjC EH puts on decls. */
11403 if (!objc_ok)
11404 pedwarn (location, 0,
11405 "comparison of distinct pointer types lacks a cast");
11406
11407 if (result_type == NULL_TREE)
11408 {
11409 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11410 result_type = build_pointer_type
11411 (build_qualified_type (void_type_node, qual));
11412 }
11413 }
11414 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11415 {
11416 result_type = type0;
11417 pedwarn (location, 0, "comparison between pointer and integer");
11418 }
11419 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11420 {
11421 result_type = type1;
11422 pedwarn (location, 0, "comparison between pointer and integer");
11423 }
11424 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11425 || truth_value_p (TREE_CODE (orig_op0)))
11426 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11427 || truth_value_p (TREE_CODE (orig_op1))))
11428 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11429 break;
11430
11431 case LE_EXPR:
11432 case GE_EXPR:
11433 case LT_EXPR:
11434 case GT_EXPR:
11435 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11436 {
11437 tree intt;
11438 if (!vector_types_compatible_elements_p (type0, type1))
11439 {
11440 error_at (location, "comparing vectors with different "
11441 "element types");
11442 return error_mark_node;
11443 }
11444
11445 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11446 {
11447 error_at (location, "comparing vectors with different "
11448 "number of elements");
11449 return error_mark_node;
11450 }
11451
11452 /* It's not precisely specified how the usual arithmetic
11453 conversions apply to the vector types. Here, we use
11454 the unsigned type if one of the operands is signed and
11455 the other one is unsigned. */
11456 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11457 {
11458 if (!TYPE_UNSIGNED (type0))
11459 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11460 else
11461 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11462 warning_at (location, OPT_Wsign_compare, "comparison between "
11463 "types %qT and %qT", type0, type1);
11464 }
11465
11466 /* Always construct signed integer vector type. */
11467 intt = c_common_type_for_size (GET_MODE_BITSIZE
11468 (TYPE_MODE (TREE_TYPE (type0))), 0);
11469 result_type = build_opaque_vector_type (intt,
11470 TYPE_VECTOR_SUBPARTS (type0));
11471 converted = 1;
11472 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11473 goto return_build_binary_op;
11474 }
11475 build_type = integer_type_node;
11476 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11477 || code0 == FIXED_POINT_TYPE)
11478 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11479 || code1 == FIXED_POINT_TYPE))
11480 short_compare = 1;
11481 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11482 {
11483 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11484 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11485 addr_space_t as_common;
11486
11487 if (comp_target_types (location, type0, type1))
11488 {
11489 result_type = common_pointer_type (type0, type1);
11490 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11491 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11492 pedwarn (location, 0,
11493 "comparison of complete and incomplete pointers");
11494 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11495 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11496 "ordered comparisons of pointers to functions");
11497 else if (null_pointer_constant_p (orig_op0)
11498 || null_pointer_constant_p (orig_op1))
11499 warning_at (location, OPT_Wextra,
11500 "ordered comparison of pointer with null pointer");
11501
11502 }
11503 else if (!addr_space_superset (as0, as1, &as_common))
11504 {
11505 error_at (location, "comparison of pointers to "
11506 "disjoint address spaces");
11507 return error_mark_node;
11508 }
11509 else
11510 {
11511 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11512 result_type = build_pointer_type
11513 (build_qualified_type (void_type_node, qual));
11514 pedwarn (location, 0,
11515 "comparison of distinct pointer types lacks a cast");
11516 }
11517 }
11518 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11519 {
11520 result_type = type0;
11521 if (pedantic)
11522 pedwarn (location, OPT_Wpedantic,
11523 "ordered comparison of pointer with integer zero");
11524 else if (extra_warnings)
11525 warning_at (location, OPT_Wextra,
11526 "ordered comparison of pointer with integer zero");
11527 }
11528 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11529 {
11530 result_type = type1;
11531 if (pedantic)
11532 pedwarn (location, OPT_Wpedantic,
11533 "ordered comparison of pointer with integer zero");
11534 else if (extra_warnings)
11535 warning_at (location, OPT_Wextra,
11536 "ordered comparison of pointer with integer zero");
11537 }
11538 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11539 {
11540 result_type = type0;
11541 pedwarn (location, 0, "comparison between pointer and integer");
11542 }
11543 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11544 {
11545 result_type = type1;
11546 pedwarn (location, 0, "comparison between pointer and integer");
11547 }
11548 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11549 || truth_value_p (TREE_CODE (orig_op0)))
11550 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11551 || truth_value_p (TREE_CODE (orig_op1))))
11552 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11553 break;
11554
11555 default:
11556 gcc_unreachable ();
11557 }
11558
11559 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11560 return error_mark_node;
11561
11562 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11563 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11564 || !vector_types_compatible_elements_p (type0, type1)))
11565 {
11566 gcc_rich_location richloc (location);
11567 richloc.maybe_add_expr (orig_op0);
11568 richloc.maybe_add_expr (orig_op1);
11569 binary_op_error (&richloc, code, type0, type1);
11570 return error_mark_node;
11571 }
11572
11573 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11574 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11575 &&
11576 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11577 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11578 {
11579 bool first_complex = (code0 == COMPLEX_TYPE);
11580 bool second_complex = (code1 == COMPLEX_TYPE);
11581 int none_complex = (!first_complex && !second_complex);
11582
11583 if (shorten || common || short_compare)
11584 {
11585 result_type = c_common_type (type0, type1);
11586 do_warn_double_promotion (result_type, type0, type1,
11587 "implicit conversion from %qT to %qT "
11588 "to match other operand of binary "
11589 "expression",
11590 location);
11591 if (result_type == error_mark_node)
11592 return error_mark_node;
11593 }
11594
11595 if (first_complex != second_complex
11596 && (code == PLUS_EXPR
11597 || code == MINUS_EXPR
11598 || code == MULT_EXPR
11599 || (code == TRUNC_DIV_EXPR && first_complex))
11600 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11601 && flag_signed_zeros)
11602 {
11603 /* An operation on mixed real/complex operands must be
11604 handled specially, but the language-independent code can
11605 more easily optimize the plain complex arithmetic if
11606 -fno-signed-zeros. */
11607 tree real_type = TREE_TYPE (result_type);
11608 tree real, imag;
11609 if (type0 != orig_type0 || type1 != orig_type1)
11610 {
11611 gcc_assert (may_need_excess_precision && common);
11612 semantic_result_type = c_common_type (orig_type0, orig_type1);
11613 }
11614 if (first_complex)
11615 {
11616 if (TREE_TYPE (op0) != result_type)
11617 op0 = convert_and_check (location, result_type, op0);
11618 if (TREE_TYPE (op1) != real_type)
11619 op1 = convert_and_check (location, real_type, op1);
11620 }
11621 else
11622 {
11623 if (TREE_TYPE (op0) != real_type)
11624 op0 = convert_and_check (location, real_type, op0);
11625 if (TREE_TYPE (op1) != result_type)
11626 op1 = convert_and_check (location, result_type, op1);
11627 }
11628 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11629 return error_mark_node;
11630 if (first_complex)
11631 {
11632 op0 = save_expr (op0);
11633 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11634 op0, true);
11635 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11636 op0, true);
11637 switch (code)
11638 {
11639 case MULT_EXPR:
11640 case TRUNC_DIV_EXPR:
11641 op1 = save_expr (op1);
11642 imag = build2 (resultcode, real_type, imag, op1);
11643 /* Fall through. */
11644 case PLUS_EXPR:
11645 case MINUS_EXPR:
11646 real = build2 (resultcode, real_type, real, op1);
11647 break;
11648 default:
11649 gcc_unreachable();
11650 }
11651 }
11652 else
11653 {
11654 op1 = save_expr (op1);
11655 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11656 op1, true);
11657 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11658 op1, true);
11659 switch (code)
11660 {
11661 case MULT_EXPR:
11662 op0 = save_expr (op0);
11663 imag = build2 (resultcode, real_type, op0, imag);
11664 /* Fall through. */
11665 case PLUS_EXPR:
11666 real = build2 (resultcode, real_type, op0, real);
11667 break;
11668 case MINUS_EXPR:
11669 real = build2 (resultcode, real_type, op0, real);
11670 imag = build1 (NEGATE_EXPR, real_type, imag);
11671 break;
11672 default:
11673 gcc_unreachable();
11674 }
11675 }
11676 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11677 goto return_build_binary_op;
11678 }
11679
11680 /* For certain operations (which identify themselves by shorten != 0)
11681 if both args were extended from the same smaller type,
11682 do the arithmetic in that type and then extend.
11683
11684 shorten !=0 and !=1 indicates a bitwise operation.
11685 For them, this optimization is safe only if
11686 both args are zero-extended or both are sign-extended.
11687 Otherwise, we might change the result.
11688 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11689 but calculated in (unsigned short) it would be (unsigned short)-1. */
11690
11691 if (shorten && none_complex)
11692 {
11693 final_type = result_type;
11694 result_type = shorten_binary_op (result_type, op0, op1,
11695 shorten == -1);
11696 }
11697
11698 /* Shifts can be shortened if shifting right. */
11699
11700 if (short_shift)
11701 {
11702 int unsigned_arg;
11703 tree arg0 = get_narrower (op0, &unsigned_arg);
11704
11705 final_type = result_type;
11706
11707 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11708 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11709
11710 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11711 && tree_int_cst_sgn (op1) > 0
11712 /* We can shorten only if the shift count is less than the
11713 number of bits in the smaller type size. */
11714 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11715 /* We cannot drop an unsigned shift after sign-extension. */
11716 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11717 {
11718 /* Do an unsigned shift if the operand was zero-extended. */
11719 result_type
11720 = c_common_signed_or_unsigned_type (unsigned_arg,
11721 TREE_TYPE (arg0));
11722 /* Convert value-to-be-shifted to that type. */
11723 if (TREE_TYPE (op0) != result_type)
11724 op0 = convert (result_type, op0);
11725 converted = 1;
11726 }
11727 }
11728
11729 /* Comparison operations are shortened too but differently.
11730 They identify themselves by setting short_compare = 1. */
11731
11732 if (short_compare)
11733 {
11734 /* Don't write &op0, etc., because that would prevent op0
11735 from being kept in a register.
11736 Instead, make copies of the our local variables and
11737 pass the copies by reference, then copy them back afterward. */
11738 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11739 enum tree_code xresultcode = resultcode;
11740 tree val
11741 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11742 &xresultcode);
11743
11744 if (val != NULL_TREE)
11745 {
11746 ret = val;
11747 goto return_build_binary_op;
11748 }
11749
11750 op0 = xop0, op1 = xop1;
11751 converted = 1;
11752 resultcode = xresultcode;
11753
11754 if (c_inhibit_evaluation_warnings == 0)
11755 {
11756 bool op0_maybe_const = true;
11757 bool op1_maybe_const = true;
11758 tree orig_op0_folded, orig_op1_folded;
11759
11760 if (in_late_binary_op)
11761 {
11762 orig_op0_folded = orig_op0;
11763 orig_op1_folded = orig_op1;
11764 }
11765 else
11766 {
11767 /* Fold for the sake of possible warnings, as in
11768 build_conditional_expr. This requires the
11769 "original" values to be folded, not just op0 and
11770 op1. */
11771 c_inhibit_evaluation_warnings++;
11772 op0 = c_fully_fold (op0, require_constant_value,
11773 &op0_maybe_const);
11774 op1 = c_fully_fold (op1, require_constant_value,
11775 &op1_maybe_const);
11776 c_inhibit_evaluation_warnings--;
11777 orig_op0_folded = c_fully_fold (orig_op0,
11778 require_constant_value,
11779 NULL);
11780 orig_op1_folded = c_fully_fold (orig_op1,
11781 require_constant_value,
11782 NULL);
11783 }
11784
11785 if (warn_sign_compare)
11786 warn_for_sign_compare (location, orig_op0_folded,
11787 orig_op1_folded, op0, op1,
11788 result_type, resultcode);
11789 if (!in_late_binary_op && !int_operands)
11790 {
11791 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11792 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11793 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11794 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11795 }
11796 }
11797 }
11798 }
11799
11800 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11801 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11802 Then the expression will be built.
11803 It will be given type FINAL_TYPE if that is nonzero;
11804 otherwise, it will be given type RESULT_TYPE. */
11805
11806 if (!result_type)
11807 {
11808 gcc_rich_location richloc (location);
11809 richloc.maybe_add_expr (orig_op0);
11810 richloc.maybe_add_expr (orig_op1);
11811 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
11812 return error_mark_node;
11813 }
11814
11815 if (build_type == NULL_TREE)
11816 {
11817 build_type = result_type;
11818 if ((type0 != orig_type0 || type1 != orig_type1)
11819 && !boolean_op)
11820 {
11821 gcc_assert (may_need_excess_precision && common);
11822 semantic_result_type = c_common_type (orig_type0, orig_type1);
11823 }
11824 }
11825
11826 if (!converted)
11827 {
11828 op0 = ep_convert_and_check (location, result_type, op0,
11829 semantic_result_type);
11830 op1 = ep_convert_and_check (location, result_type, op1,
11831 semantic_result_type);
11832
11833 /* This can happen if one operand has a vector type, and the other
11834 has a different type. */
11835 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11836 return error_mark_node;
11837 }
11838
11839 if (sanitize_flags_p ((SANITIZE_SHIFT
11840 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
11841 && current_function_decl != NULL_TREE
11842 && (doing_div_or_mod || doing_shift)
11843 && !require_constant_value)
11844 {
11845 /* OP0 and/or OP1 might have side-effects. */
11846 op0 = save_expr (op0);
11847 op1 = save_expr (op1);
11848 op0 = c_fully_fold (op0, false, NULL);
11849 op1 = c_fully_fold (op1, false, NULL);
11850 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
11851 | SANITIZE_FLOAT_DIVIDE))))
11852 instrument_expr = ubsan_instrument_division (location, op0, op1);
11853 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
11854 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11855 }
11856
11857 /* Treat expressions in initializers specially as they can't trap. */
11858 if (int_const_or_overflow)
11859 ret = (require_constant_value
11860 ? fold_build2_initializer_loc (location, resultcode, build_type,
11861 op0, op1)
11862 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11863 else
11864 ret = build2 (resultcode, build_type, op0, op1);
11865 if (final_type != NULL_TREE)
11866 ret = convert (final_type, ret);
11867
11868 return_build_binary_op:
11869 gcc_assert (ret != error_mark_node);
11870 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11871 ret = (int_operands
11872 ? note_integer_operands (ret)
11873 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11874 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11875 && !in_late_binary_op)
11876 ret = note_integer_operands (ret);
11877 protected_set_expr_location (ret, location);
11878
11879 if (instrument_expr != NULL)
11880 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11881 instrument_expr, ret);
11882
11883 if (semantic_result_type)
11884 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
11885 semantic_result_type, ret);
11886
11887 return ret;
11888 }
11889
11890
11891 /* Convert EXPR to be a truth-value, validating its type for this
11892 purpose. LOCATION is the source location for the expression. */
11893
11894 tree
11895 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11896 {
11897 bool int_const, int_operands;
11898
11899 switch (TREE_CODE (TREE_TYPE (expr)))
11900 {
11901 case ARRAY_TYPE:
11902 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11903 return error_mark_node;
11904
11905 case RECORD_TYPE:
11906 error_at (location, "used struct type value where scalar is required");
11907 return error_mark_node;
11908
11909 case UNION_TYPE:
11910 error_at (location, "used union type value where scalar is required");
11911 return error_mark_node;
11912
11913 case VOID_TYPE:
11914 error_at (location, "void value not ignored as it ought to be");
11915 return error_mark_node;
11916
11917 case POINTER_TYPE:
11918 if (reject_gcc_builtin (expr))
11919 return error_mark_node;
11920 break;
11921
11922 case FUNCTION_TYPE:
11923 gcc_unreachable ();
11924
11925 case VECTOR_TYPE:
11926 error_at (location, "used vector type where scalar is required");
11927 return error_mark_node;
11928
11929 default:
11930 break;
11931 }
11932
11933 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11934 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11935 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11936 {
11937 expr = remove_c_maybe_const_expr (expr);
11938 expr = build2 (NE_EXPR, integer_type_node, expr,
11939 convert (TREE_TYPE (expr), integer_zero_node));
11940 expr = note_integer_operands (expr);
11941 }
11942 else
11943 /* ??? Should we also give an error for vectors rather than leaving
11944 those to give errors later? */
11945 expr = c_common_truthvalue_conversion (location, expr);
11946
11947 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11948 {
11949 if (TREE_OVERFLOW (expr))
11950 return expr;
11951 else
11952 return note_integer_operands (expr);
11953 }
11954 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11955 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11956 return expr;
11957 }
11958 \f
11959
11960 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11961 required. */
11962
11963 tree
11964 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11965 {
11966 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11967 {
11968 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11969 /* Executing a compound literal inside a function reinitializes
11970 it. */
11971 if (!TREE_STATIC (decl))
11972 *se = true;
11973 return decl;
11974 }
11975 else
11976 return expr;
11977 }
11978 \f
11979 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11980 statement. LOC is the location of the construct. */
11981
11982 tree
11983 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11984 tree clauses)
11985 {
11986 body = c_end_compound_stmt (loc, body, true);
11987
11988 tree stmt = make_node (code);
11989 TREE_TYPE (stmt) = void_type_node;
11990 OMP_BODY (stmt) = body;
11991 OMP_CLAUSES (stmt) = clauses;
11992 SET_EXPR_LOCATION (stmt, loc);
11993
11994 return add_stmt (stmt);
11995 }
11996
11997 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11998 statement. LOC is the location of the OACC_DATA. */
11999
12000 tree
12001 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12002 {
12003 tree stmt;
12004
12005 block = c_end_compound_stmt (loc, block, true);
12006
12007 stmt = make_node (OACC_DATA);
12008 TREE_TYPE (stmt) = void_type_node;
12009 OACC_DATA_CLAUSES (stmt) = clauses;
12010 OACC_DATA_BODY (stmt) = block;
12011 SET_EXPR_LOCATION (stmt, loc);
12012
12013 return add_stmt (stmt);
12014 }
12015
12016 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12017 statement. LOC is the location of the OACC_HOST_DATA. */
12018
12019 tree
12020 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12021 {
12022 tree stmt;
12023
12024 block = c_end_compound_stmt (loc, block, true);
12025
12026 stmt = make_node (OACC_HOST_DATA);
12027 TREE_TYPE (stmt) = void_type_node;
12028 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12029 OACC_HOST_DATA_BODY (stmt) = block;
12030 SET_EXPR_LOCATION (stmt, loc);
12031
12032 return add_stmt (stmt);
12033 }
12034
12035 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12036
12037 tree
12038 c_begin_omp_parallel (void)
12039 {
12040 tree block;
12041
12042 keep_next_level ();
12043 block = c_begin_compound_stmt (true);
12044
12045 return block;
12046 }
12047
12048 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12049 statement. LOC is the location of the OMP_PARALLEL. */
12050
12051 tree
12052 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12053 {
12054 tree stmt;
12055
12056 block = c_end_compound_stmt (loc, block, true);
12057
12058 stmt = make_node (OMP_PARALLEL);
12059 TREE_TYPE (stmt) = void_type_node;
12060 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12061 OMP_PARALLEL_BODY (stmt) = block;
12062 SET_EXPR_LOCATION (stmt, loc);
12063
12064 return add_stmt (stmt);
12065 }
12066
12067 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12068
12069 tree
12070 c_begin_omp_task (void)
12071 {
12072 tree block;
12073
12074 keep_next_level ();
12075 block = c_begin_compound_stmt (true);
12076
12077 return block;
12078 }
12079
12080 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12081 statement. LOC is the location of the #pragma. */
12082
12083 tree
12084 c_finish_omp_task (location_t loc, tree clauses, tree block)
12085 {
12086 tree stmt;
12087
12088 block = c_end_compound_stmt (loc, block, true);
12089
12090 stmt = make_node (OMP_TASK);
12091 TREE_TYPE (stmt) = void_type_node;
12092 OMP_TASK_CLAUSES (stmt) = clauses;
12093 OMP_TASK_BODY (stmt) = block;
12094 SET_EXPR_LOCATION (stmt, loc);
12095
12096 return add_stmt (stmt);
12097 }
12098
12099 /* Generate GOMP_cancel call for #pragma omp cancel. */
12100
12101 void
12102 c_finish_omp_cancel (location_t loc, tree clauses)
12103 {
12104 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12105 int mask = 0;
12106 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12107 mask = 1;
12108 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12109 mask = 2;
12110 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12111 mask = 4;
12112 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12113 mask = 8;
12114 else
12115 {
12116 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12117 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12118 "clauses");
12119 return;
12120 }
12121 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12122 if (ifc != NULL_TREE)
12123 {
12124 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12125 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12126 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12127 build_zero_cst (type));
12128 }
12129 else
12130 ifc = boolean_true_node;
12131 tree stmt = build_call_expr_loc (loc, fn, 2,
12132 build_int_cst (integer_type_node, mask),
12133 ifc);
12134 add_stmt (stmt);
12135 }
12136
12137 /* Generate GOMP_cancellation_point call for
12138 #pragma omp cancellation point. */
12139
12140 void
12141 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12142 {
12143 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12144 int mask = 0;
12145 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12146 mask = 1;
12147 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12148 mask = 2;
12149 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12150 mask = 4;
12151 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12152 mask = 8;
12153 else
12154 {
12155 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12156 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12157 "clauses");
12158 return;
12159 }
12160 tree stmt = build_call_expr_loc (loc, fn, 1,
12161 build_int_cst (integer_type_node, mask));
12162 add_stmt (stmt);
12163 }
12164
12165 /* Helper function for handle_omp_array_sections. Called recursively
12166 to handle multiple array-section-subscripts. C is the clause,
12167 T current expression (initially OMP_CLAUSE_DECL), which is either
12168 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12169 expression if specified, TREE_VALUE length expression if specified,
12170 TREE_CHAIN is what it has been specified after, or some decl.
12171 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12172 set to true if any of the array-section-subscript could have length
12173 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12174 first array-section-subscript which is known not to have length
12175 of one. Given say:
12176 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12177 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12178 all are or may have length of 1, array-section-subscript [:2] is the
12179 first one known not to have length 1. For array-section-subscript
12180 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12181 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12182 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12183 case though, as some lengths could be zero. */
12184
12185 static tree
12186 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12187 bool &maybe_zero_len, unsigned int &first_non_one,
12188 enum c_omp_region_type ort)
12189 {
12190 tree ret, low_bound, length, type;
12191 if (TREE_CODE (t) != TREE_LIST)
12192 {
12193 if (error_operand_p (t))
12194 return error_mark_node;
12195 ret = t;
12196 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12197 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12198 {
12199 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12200 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12201 return error_mark_node;
12202 }
12203 if (TREE_CODE (t) == COMPONENT_REF
12204 && ort == C_ORT_OMP
12205 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12206 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12207 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12208 {
12209 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12210 {
12211 error_at (OMP_CLAUSE_LOCATION (c),
12212 "bit-field %qE in %qs clause",
12213 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12214 return error_mark_node;
12215 }
12216 while (TREE_CODE (t) == COMPONENT_REF)
12217 {
12218 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12219 {
12220 error_at (OMP_CLAUSE_LOCATION (c),
12221 "%qE is a member of a union", t);
12222 return error_mark_node;
12223 }
12224 t = TREE_OPERAND (t, 0);
12225 }
12226 }
12227 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12228 {
12229 if (DECL_P (t))
12230 error_at (OMP_CLAUSE_LOCATION (c),
12231 "%qD is not a variable in %qs clause", t,
12232 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12233 else
12234 error_at (OMP_CLAUSE_LOCATION (c),
12235 "%qE is not a variable in %qs clause", t,
12236 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12237 return error_mark_node;
12238 }
12239 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12240 && TYPE_ATOMIC (TREE_TYPE (t)))
12241 {
12242 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12243 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12244 return error_mark_node;
12245 }
12246 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12247 && VAR_P (t)
12248 && DECL_THREAD_LOCAL_P (t))
12249 {
12250 error_at (OMP_CLAUSE_LOCATION (c),
12251 "%qD is threadprivate variable in %qs clause", t,
12252 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12253 return error_mark_node;
12254 }
12255 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12256 && TYPE_ATOMIC (TREE_TYPE (t))
12257 && POINTER_TYPE_P (TREE_TYPE (t)))
12258 {
12259 /* If the array section is pointer based and the pointer
12260 itself is _Atomic qualified, we need to atomically load
12261 the pointer. */
12262 c_expr expr;
12263 memset (&expr, 0, sizeof (expr));
12264 expr.value = ret;
12265 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12266 expr, false, false);
12267 ret = expr.value;
12268 }
12269 return ret;
12270 }
12271
12272 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12273 maybe_zero_len, first_non_one, ort);
12274 if (ret == error_mark_node || ret == NULL_TREE)
12275 return ret;
12276
12277 type = TREE_TYPE (ret);
12278 low_bound = TREE_PURPOSE (t);
12279 length = TREE_VALUE (t);
12280
12281 if (low_bound == error_mark_node || length == error_mark_node)
12282 return error_mark_node;
12283
12284 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12285 {
12286 error_at (OMP_CLAUSE_LOCATION (c),
12287 "low bound %qE of array section does not have integral type",
12288 low_bound);
12289 return error_mark_node;
12290 }
12291 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12292 {
12293 error_at (OMP_CLAUSE_LOCATION (c),
12294 "length %qE of array section does not have integral type",
12295 length);
12296 return error_mark_node;
12297 }
12298 if (low_bound
12299 && TREE_CODE (low_bound) == INTEGER_CST
12300 && TYPE_PRECISION (TREE_TYPE (low_bound))
12301 > TYPE_PRECISION (sizetype))
12302 low_bound = fold_convert (sizetype, low_bound);
12303 if (length
12304 && TREE_CODE (length) == INTEGER_CST
12305 && TYPE_PRECISION (TREE_TYPE (length))
12306 > TYPE_PRECISION (sizetype))
12307 length = fold_convert (sizetype, length);
12308 if (low_bound == NULL_TREE)
12309 low_bound = integer_zero_node;
12310
12311 if (length != NULL_TREE)
12312 {
12313 if (!integer_nonzerop (length))
12314 {
12315 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12316 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12317 {
12318 if (integer_zerop (length))
12319 {
12320 error_at (OMP_CLAUSE_LOCATION (c),
12321 "zero length array section in %qs clause",
12322 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12323 return error_mark_node;
12324 }
12325 }
12326 else
12327 maybe_zero_len = true;
12328 }
12329 if (first_non_one == types.length ()
12330 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12331 first_non_one++;
12332 }
12333 if (TREE_CODE (type) == ARRAY_TYPE)
12334 {
12335 if (length == NULL_TREE
12336 && (TYPE_DOMAIN (type) == NULL_TREE
12337 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12338 {
12339 error_at (OMP_CLAUSE_LOCATION (c),
12340 "for unknown bound array type length expression must "
12341 "be specified");
12342 return error_mark_node;
12343 }
12344 if (TREE_CODE (low_bound) == INTEGER_CST
12345 && tree_int_cst_sgn (low_bound) == -1)
12346 {
12347 error_at (OMP_CLAUSE_LOCATION (c),
12348 "negative low bound in array section in %qs clause",
12349 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12350 return error_mark_node;
12351 }
12352 if (length != NULL_TREE
12353 && TREE_CODE (length) == INTEGER_CST
12354 && tree_int_cst_sgn (length) == -1)
12355 {
12356 error_at (OMP_CLAUSE_LOCATION (c),
12357 "negative length in array section in %qs clause",
12358 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12359 return error_mark_node;
12360 }
12361 if (TYPE_DOMAIN (type)
12362 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12363 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12364 == INTEGER_CST)
12365 {
12366 tree size
12367 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
12368 size = size_binop (PLUS_EXPR, size, size_one_node);
12369 if (TREE_CODE (low_bound) == INTEGER_CST)
12370 {
12371 if (tree_int_cst_lt (size, low_bound))
12372 {
12373 error_at (OMP_CLAUSE_LOCATION (c),
12374 "low bound %qE above array section size "
12375 "in %qs clause", low_bound,
12376 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12377 return error_mark_node;
12378 }
12379 if (tree_int_cst_equal (size, low_bound))
12380 {
12381 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12382 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12383 {
12384 error_at (OMP_CLAUSE_LOCATION (c),
12385 "zero length array section in %qs clause",
12386 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12387 return error_mark_node;
12388 }
12389 maybe_zero_len = true;
12390 }
12391 else if (length == NULL_TREE
12392 && first_non_one == types.length ()
12393 && tree_int_cst_equal
12394 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12395 low_bound))
12396 first_non_one++;
12397 }
12398 else if (length == NULL_TREE)
12399 {
12400 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12401 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12402 maybe_zero_len = true;
12403 if (first_non_one == types.length ())
12404 first_non_one++;
12405 }
12406 if (length && TREE_CODE (length) == INTEGER_CST)
12407 {
12408 if (tree_int_cst_lt (size, length))
12409 {
12410 error_at (OMP_CLAUSE_LOCATION (c),
12411 "length %qE above array section size "
12412 "in %qs clause", length,
12413 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12414 return error_mark_node;
12415 }
12416 if (TREE_CODE (low_bound) == INTEGER_CST)
12417 {
12418 tree lbpluslen
12419 = size_binop (PLUS_EXPR,
12420 fold_convert (sizetype, low_bound),
12421 fold_convert (sizetype, length));
12422 if (TREE_CODE (lbpluslen) == INTEGER_CST
12423 && tree_int_cst_lt (size, lbpluslen))
12424 {
12425 error_at (OMP_CLAUSE_LOCATION (c),
12426 "high bound %qE above array section size "
12427 "in %qs clause", lbpluslen,
12428 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12429 return error_mark_node;
12430 }
12431 }
12432 }
12433 }
12434 else if (length == NULL_TREE)
12435 {
12436 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12437 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12438 maybe_zero_len = true;
12439 if (first_non_one == types.length ())
12440 first_non_one++;
12441 }
12442
12443 /* For [lb:] we will need to evaluate lb more than once. */
12444 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12445 {
12446 tree lb = save_expr (low_bound);
12447 if (lb != low_bound)
12448 {
12449 TREE_PURPOSE (t) = lb;
12450 low_bound = lb;
12451 }
12452 }
12453 }
12454 else if (TREE_CODE (type) == POINTER_TYPE)
12455 {
12456 if (length == NULL_TREE)
12457 {
12458 error_at (OMP_CLAUSE_LOCATION (c),
12459 "for pointer type length expression must be specified");
12460 return error_mark_node;
12461 }
12462 if (length != NULL_TREE
12463 && TREE_CODE (length) == INTEGER_CST
12464 && tree_int_cst_sgn (length) == -1)
12465 {
12466 error_at (OMP_CLAUSE_LOCATION (c),
12467 "negative length in array section in %qs clause",
12468 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12469 return error_mark_node;
12470 }
12471 /* If there is a pointer type anywhere but in the very first
12472 array-section-subscript, the array section can't be contiguous. */
12473 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12474 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12475 {
12476 error_at (OMP_CLAUSE_LOCATION (c),
12477 "array section is not contiguous in %qs clause",
12478 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12479 return error_mark_node;
12480 }
12481 }
12482 else
12483 {
12484 error_at (OMP_CLAUSE_LOCATION (c),
12485 "%qE does not have pointer or array type", ret);
12486 return error_mark_node;
12487 }
12488 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12489 types.safe_push (TREE_TYPE (ret));
12490 /* We will need to evaluate lb more than once. */
12491 tree lb = save_expr (low_bound);
12492 if (lb != low_bound)
12493 {
12494 TREE_PURPOSE (t) = lb;
12495 low_bound = lb;
12496 }
12497 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12498 return ret;
12499 }
12500
12501 /* Handle array sections for clause C. */
12502
12503 static bool
12504 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12505 {
12506 bool maybe_zero_len = false;
12507 unsigned int first_non_one = 0;
12508 auto_vec<tree, 10> types;
12509 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12510 maybe_zero_len, first_non_one,
12511 ort);
12512 if (first == error_mark_node)
12513 return true;
12514 if (first == NULL_TREE)
12515 return false;
12516 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12517 {
12518 tree t = OMP_CLAUSE_DECL (c);
12519 tree tem = NULL_TREE;
12520 /* Need to evaluate side effects in the length expressions
12521 if any. */
12522 while (TREE_CODE (t) == TREE_LIST)
12523 {
12524 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12525 {
12526 if (tem == NULL_TREE)
12527 tem = TREE_VALUE (t);
12528 else
12529 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12530 TREE_VALUE (t), tem);
12531 }
12532 t = TREE_CHAIN (t);
12533 }
12534 if (tem)
12535 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12536 first = c_fully_fold (first, false, NULL);
12537 OMP_CLAUSE_DECL (c) = first;
12538 }
12539 else
12540 {
12541 unsigned int num = types.length (), i;
12542 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12543 tree condition = NULL_TREE;
12544
12545 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12546 maybe_zero_len = true;
12547
12548 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12549 t = TREE_CHAIN (t))
12550 {
12551 tree low_bound = TREE_PURPOSE (t);
12552 tree length = TREE_VALUE (t);
12553
12554 i--;
12555 if (low_bound
12556 && TREE_CODE (low_bound) == INTEGER_CST
12557 && TYPE_PRECISION (TREE_TYPE (low_bound))
12558 > TYPE_PRECISION (sizetype))
12559 low_bound = fold_convert (sizetype, low_bound);
12560 if (length
12561 && TREE_CODE (length) == INTEGER_CST
12562 && TYPE_PRECISION (TREE_TYPE (length))
12563 > TYPE_PRECISION (sizetype))
12564 length = fold_convert (sizetype, length);
12565 if (low_bound == NULL_TREE)
12566 low_bound = integer_zero_node;
12567 if (!maybe_zero_len && i > first_non_one)
12568 {
12569 if (integer_nonzerop (low_bound))
12570 goto do_warn_noncontiguous;
12571 if (length != NULL_TREE
12572 && TREE_CODE (length) == INTEGER_CST
12573 && TYPE_DOMAIN (types[i])
12574 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12575 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12576 == INTEGER_CST)
12577 {
12578 tree size;
12579 size = size_binop (PLUS_EXPR,
12580 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12581 size_one_node);
12582 if (!tree_int_cst_equal (length, size))
12583 {
12584 do_warn_noncontiguous:
12585 error_at (OMP_CLAUSE_LOCATION (c),
12586 "array section is not contiguous in %qs "
12587 "clause",
12588 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12589 return true;
12590 }
12591 }
12592 if (length != NULL_TREE
12593 && TREE_SIDE_EFFECTS (length))
12594 {
12595 if (side_effects == NULL_TREE)
12596 side_effects = length;
12597 else
12598 side_effects = build2 (COMPOUND_EXPR,
12599 TREE_TYPE (side_effects),
12600 length, side_effects);
12601 }
12602 }
12603 else
12604 {
12605 tree l;
12606
12607 if (i > first_non_one
12608 && ((length && integer_nonzerop (length))
12609 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12610 continue;
12611 if (length)
12612 l = fold_convert (sizetype, length);
12613 else
12614 {
12615 l = size_binop (PLUS_EXPR,
12616 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12617 size_one_node);
12618 l = size_binop (MINUS_EXPR, l,
12619 fold_convert (sizetype, low_bound));
12620 }
12621 if (i > first_non_one)
12622 {
12623 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12624 size_zero_node);
12625 if (condition == NULL_TREE)
12626 condition = l;
12627 else
12628 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12629 l, condition);
12630 }
12631 else if (size == NULL_TREE)
12632 {
12633 size = size_in_bytes (TREE_TYPE (types[i]));
12634 tree eltype = TREE_TYPE (types[num - 1]);
12635 while (TREE_CODE (eltype) == ARRAY_TYPE)
12636 eltype = TREE_TYPE (eltype);
12637 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12638 {
12639 if (integer_zerop (size)
12640 || integer_zerop (size_in_bytes (eltype)))
12641 {
12642 error_at (OMP_CLAUSE_LOCATION (c),
12643 "zero length array section in %qs clause",
12644 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12645 return error_mark_node;
12646 }
12647 size = size_binop (EXACT_DIV_EXPR, size,
12648 size_in_bytes (eltype));
12649 }
12650 size = size_binop (MULT_EXPR, size, l);
12651 if (condition)
12652 size = fold_build3 (COND_EXPR, sizetype, condition,
12653 size, size_zero_node);
12654 }
12655 else
12656 size = size_binop (MULT_EXPR, size, l);
12657 }
12658 }
12659 if (side_effects)
12660 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12661 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12662 {
12663 size = size_binop (MINUS_EXPR, size, size_one_node);
12664 size = c_fully_fold (size, false, NULL);
12665 tree index_type = build_index_type (size);
12666 tree eltype = TREE_TYPE (first);
12667 while (TREE_CODE (eltype) == ARRAY_TYPE)
12668 eltype = TREE_TYPE (eltype);
12669 tree type = build_array_type (eltype, index_type);
12670 tree ptype = build_pointer_type (eltype);
12671 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12672 t = build_fold_addr_expr (t);
12673 tree t2 = build_fold_addr_expr (first);
12674 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12675 ptrdiff_type_node, t2);
12676 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12677 ptrdiff_type_node, t2,
12678 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12679 ptrdiff_type_node, t));
12680 t2 = c_fully_fold (t2, false, NULL);
12681 if (tree_fits_shwi_p (t2))
12682 t = build2 (MEM_REF, type, t,
12683 build_int_cst (ptype, tree_to_shwi (t2)));
12684 else
12685 {
12686 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12687 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12688 TREE_TYPE (t), t, t2);
12689 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12690 }
12691 OMP_CLAUSE_DECL (c) = t;
12692 return false;
12693 }
12694 first = c_fully_fold (first, false, NULL);
12695 OMP_CLAUSE_DECL (c) = first;
12696 if (size)
12697 size = c_fully_fold (size, false, NULL);
12698 OMP_CLAUSE_SIZE (c) = size;
12699 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12700 || (TREE_CODE (t) == COMPONENT_REF
12701 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12702 return false;
12703 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12704 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
12705 switch (OMP_CLAUSE_MAP_KIND (c))
12706 {
12707 case GOMP_MAP_ALLOC:
12708 case GOMP_MAP_TO:
12709 case GOMP_MAP_FROM:
12710 case GOMP_MAP_TOFROM:
12711 case GOMP_MAP_ALWAYS_TO:
12712 case GOMP_MAP_ALWAYS_FROM:
12713 case GOMP_MAP_ALWAYS_TOFROM:
12714 case GOMP_MAP_RELEASE:
12715 case GOMP_MAP_DELETE:
12716 case GOMP_MAP_FORCE_TO:
12717 case GOMP_MAP_FORCE_FROM:
12718 case GOMP_MAP_FORCE_TOFROM:
12719 case GOMP_MAP_FORCE_PRESENT:
12720 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12721 break;
12722 default:
12723 break;
12724 }
12725 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12726 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
12727 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12728 else if (TREE_CODE (t) == COMPONENT_REF)
12729 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12730 else
12731 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12732 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12733 && !c_mark_addressable (t))
12734 return false;
12735 OMP_CLAUSE_DECL (c2) = t;
12736 t = build_fold_addr_expr (first);
12737 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12738 tree ptr = OMP_CLAUSE_DECL (c2);
12739 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12740 ptr = build_fold_addr_expr (ptr);
12741 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12742 ptrdiff_type_node, t,
12743 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12744 ptrdiff_type_node, ptr));
12745 t = c_fully_fold (t, false, NULL);
12746 OMP_CLAUSE_SIZE (c2) = t;
12747 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12748 OMP_CLAUSE_CHAIN (c) = c2;
12749 }
12750 return false;
12751 }
12752
12753 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12754 an inline call. But, remap
12755 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12756 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12757
12758 static tree
12759 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12760 tree decl, tree placeholder)
12761 {
12762 copy_body_data id;
12763 hash_map<tree, tree> decl_map;
12764
12765 decl_map.put (omp_decl1, placeholder);
12766 decl_map.put (omp_decl2, decl);
12767 memset (&id, 0, sizeof (id));
12768 id.src_fn = DECL_CONTEXT (omp_decl1);
12769 id.dst_fn = current_function_decl;
12770 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12771 id.decl_map = &decl_map;
12772
12773 id.copy_decl = copy_decl_no_change;
12774 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12775 id.transform_new_cfg = true;
12776 id.transform_return_to_modify = false;
12777 id.transform_lang_insert_block = NULL;
12778 id.eh_lp_nr = 0;
12779 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12780 return stmt;
12781 }
12782
12783 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12784 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12785
12786 static tree
12787 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12788 {
12789 if (*tp == (tree) data)
12790 return *tp;
12791 return NULL_TREE;
12792 }
12793
12794 /* For all elements of CLAUSES, validate them against their constraints.
12795 Remove any elements from the list that are invalid. */
12796
12797 tree
12798 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
12799 {
12800 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12801 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
12802 tree c, t, type, *pc;
12803 tree simdlen = NULL_TREE, safelen = NULL_TREE;
12804 bool branch_seen = false;
12805 bool copyprivate_seen = false;
12806 bool linear_variable_step_check = false;
12807 tree *nowait_clause = NULL;
12808 bool ordered_seen = false;
12809 tree schedule_clause = NULL_TREE;
12810 bool oacc_async = false;
12811
12812 bitmap_obstack_initialize (NULL);
12813 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12814 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12815 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12816 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12817 bitmap_initialize (&map_head, &bitmap_default_obstack);
12818 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
12819 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
12820
12821 if (ort & C_ORT_ACC)
12822 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12823 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12824 {
12825 oacc_async = true;
12826 break;
12827 }
12828
12829 for (pc = &clauses, c = clauses; c ; c = *pc)
12830 {
12831 bool remove = false;
12832 bool need_complete = false;
12833 bool need_implicitly_determined = false;
12834
12835 switch (OMP_CLAUSE_CODE (c))
12836 {
12837 case OMP_CLAUSE_SHARED:
12838 need_implicitly_determined = true;
12839 goto check_dup_generic;
12840
12841 case OMP_CLAUSE_PRIVATE:
12842 need_complete = true;
12843 need_implicitly_determined = true;
12844 goto check_dup_generic;
12845
12846 case OMP_CLAUSE_REDUCTION:
12847 need_implicitly_determined = true;
12848 t = OMP_CLAUSE_DECL (c);
12849 if (TREE_CODE (t) == TREE_LIST)
12850 {
12851 if (handle_omp_array_sections (c, ort))
12852 {
12853 remove = true;
12854 break;
12855 }
12856
12857 t = OMP_CLAUSE_DECL (c);
12858 }
12859 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
12860 if (t == error_mark_node)
12861 {
12862 remove = true;
12863 break;
12864 }
12865 if (oacc_async)
12866 c_mark_addressable (t);
12867 type = TREE_TYPE (t);
12868 if (TREE_CODE (t) == MEM_REF)
12869 type = TREE_TYPE (type);
12870 if (TREE_CODE (type) == ARRAY_TYPE)
12871 {
12872 tree oatype = type;
12873 gcc_assert (TREE_CODE (t) != MEM_REF);
12874 while (TREE_CODE (type) == ARRAY_TYPE)
12875 type = TREE_TYPE (type);
12876 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12877 {
12878 error_at (OMP_CLAUSE_LOCATION (c),
12879 "%qD in %<reduction%> clause is a zero size array",
12880 t);
12881 remove = true;
12882 break;
12883 }
12884 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12885 TYPE_SIZE_UNIT (type));
12886 if (integer_zerop (size))
12887 {
12888 error_at (OMP_CLAUSE_LOCATION (c),
12889 "%qD in %<reduction%> clause is a zero size array",
12890 t);
12891 remove = true;
12892 break;
12893 }
12894 size = size_binop (MINUS_EXPR, size, size_one_node);
12895 tree index_type = build_index_type (size);
12896 tree atype = build_array_type (type, index_type);
12897 tree ptype = build_pointer_type (type);
12898 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12899 t = build_fold_addr_expr (t);
12900 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12901 OMP_CLAUSE_DECL (c) = t;
12902 }
12903 if (TYPE_ATOMIC (type))
12904 {
12905 error_at (OMP_CLAUSE_LOCATION (c),
12906 "%<_Atomic%> %qE in %<reduction%> clause", t);
12907 remove = true;
12908 break;
12909 }
12910 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12911 && (FLOAT_TYPE_P (type)
12912 || TREE_CODE (type) == COMPLEX_TYPE))
12913 {
12914 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12915 const char *r_name = NULL;
12916
12917 switch (r_code)
12918 {
12919 case PLUS_EXPR:
12920 case MULT_EXPR:
12921 case MINUS_EXPR:
12922 break;
12923 case MIN_EXPR:
12924 if (TREE_CODE (type) == COMPLEX_TYPE)
12925 r_name = "min";
12926 break;
12927 case MAX_EXPR:
12928 if (TREE_CODE (type) == COMPLEX_TYPE)
12929 r_name = "max";
12930 break;
12931 case BIT_AND_EXPR:
12932 r_name = "&";
12933 break;
12934 case BIT_XOR_EXPR:
12935 r_name = "^";
12936 break;
12937 case BIT_IOR_EXPR:
12938 r_name = "|";
12939 break;
12940 case TRUTH_ANDIF_EXPR:
12941 if (FLOAT_TYPE_P (type))
12942 r_name = "&&";
12943 break;
12944 case TRUTH_ORIF_EXPR:
12945 if (FLOAT_TYPE_P (type))
12946 r_name = "||";
12947 break;
12948 default:
12949 gcc_unreachable ();
12950 }
12951 if (r_name)
12952 {
12953 error_at (OMP_CLAUSE_LOCATION (c),
12954 "%qE has invalid type for %<reduction(%s)%>",
12955 t, r_name);
12956 remove = true;
12957 break;
12958 }
12959 }
12960 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12961 {
12962 error_at (OMP_CLAUSE_LOCATION (c),
12963 "user defined reduction not found for %qE", t);
12964 remove = true;
12965 break;
12966 }
12967 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12968 {
12969 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12970 type = TYPE_MAIN_VARIANT (type);
12971 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12972 VAR_DECL, NULL_TREE, type);
12973 tree decl_placeholder = NULL_TREE;
12974 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12975 DECL_ARTIFICIAL (placeholder) = 1;
12976 DECL_IGNORED_P (placeholder) = 1;
12977 if (TREE_CODE (t) == MEM_REF)
12978 {
12979 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12980 VAR_DECL, NULL_TREE, type);
12981 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12982 DECL_ARTIFICIAL (decl_placeholder) = 1;
12983 DECL_IGNORED_P (decl_placeholder) = 1;
12984 }
12985 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12986 c_mark_addressable (placeholder);
12987 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12988 c_mark_addressable (decl_placeholder ? decl_placeholder
12989 : OMP_CLAUSE_DECL (c));
12990 OMP_CLAUSE_REDUCTION_MERGE (c)
12991 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12992 TREE_VEC_ELT (list, 0),
12993 TREE_VEC_ELT (list, 1),
12994 decl_placeholder ? decl_placeholder
12995 : OMP_CLAUSE_DECL (c), placeholder);
12996 OMP_CLAUSE_REDUCTION_MERGE (c)
12997 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12998 void_type_node, NULL_TREE,
12999 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
13000 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13001 if (TREE_VEC_LENGTH (list) == 6)
13002 {
13003 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
13004 c_mark_addressable (decl_placeholder ? decl_placeholder
13005 : OMP_CLAUSE_DECL (c));
13006 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13007 c_mark_addressable (placeholder);
13008 tree init = TREE_VEC_ELT (list, 5);
13009 if (init == error_mark_node)
13010 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13011 OMP_CLAUSE_REDUCTION_INIT (c)
13012 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13013 TREE_VEC_ELT (list, 3),
13014 decl_placeholder ? decl_placeholder
13015 : OMP_CLAUSE_DECL (c), placeholder);
13016 if (TREE_VEC_ELT (list, 5) == error_mark_node)
13017 {
13018 tree v = decl_placeholder ? decl_placeholder : t;
13019 OMP_CLAUSE_REDUCTION_INIT (c)
13020 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13021 OMP_CLAUSE_REDUCTION_INIT (c));
13022 }
13023 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13024 c_find_omp_placeholder_r,
13025 placeholder, NULL))
13026 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13027 }
13028 else
13029 {
13030 tree init;
13031 tree v = decl_placeholder ? decl_placeholder : t;
13032 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13033 init = build_constructor (TREE_TYPE (v), NULL);
13034 else
13035 init = fold_convert (TREE_TYPE (v), integer_zero_node);
13036 OMP_CLAUSE_REDUCTION_INIT (c)
13037 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
13038 }
13039 OMP_CLAUSE_REDUCTION_INIT (c)
13040 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13041 void_type_node, NULL_TREE,
13042 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13043 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13044 }
13045 if (TREE_CODE (t) == MEM_REF)
13046 {
13047 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13048 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13049 != INTEGER_CST)
13050 {
13051 sorry ("variable length element type in array "
13052 "%<reduction%> clause");
13053 remove = true;
13054 break;
13055 }
13056 t = TREE_OPERAND (t, 0);
13057 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13058 t = TREE_OPERAND (t, 0);
13059 if (TREE_CODE (t) == ADDR_EXPR)
13060 t = TREE_OPERAND (t, 0);
13061 }
13062 goto check_dup_generic_t;
13063
13064 case OMP_CLAUSE_COPYPRIVATE:
13065 copyprivate_seen = true;
13066 if (nowait_clause)
13067 {
13068 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13069 "%<nowait%> clause must not be used together "
13070 "with %<copyprivate%>");
13071 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13072 nowait_clause = NULL;
13073 }
13074 goto check_dup_generic;
13075
13076 case OMP_CLAUSE_COPYIN:
13077 t = OMP_CLAUSE_DECL (c);
13078 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
13079 {
13080 error_at (OMP_CLAUSE_LOCATION (c),
13081 "%qE must be %<threadprivate%> for %<copyin%>", t);
13082 remove = true;
13083 break;
13084 }
13085 goto check_dup_generic;
13086
13087 case OMP_CLAUSE_LINEAR:
13088 if (ort != C_ORT_OMP_DECLARE_SIMD)
13089 need_implicitly_determined = true;
13090 t = OMP_CLAUSE_DECL (c);
13091 if (ort != C_ORT_OMP_DECLARE_SIMD
13092 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13093 {
13094 error_at (OMP_CLAUSE_LOCATION (c),
13095 "modifier should not be specified in %<linear%> "
13096 "clause on %<simd%> or %<for%> constructs");
13097 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13098 }
13099 if (ort & C_ORT_CILK)
13100 {
13101 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13102 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13103 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13104 {
13105 error_at (OMP_CLAUSE_LOCATION (c),
13106 "linear clause applied to non-integral, "
13107 "non-floating, non-pointer variable with type %qT",
13108 TREE_TYPE (t));
13109 remove = true;
13110 break;
13111 }
13112 }
13113 else
13114 {
13115 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13116 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13117 {
13118 error_at (OMP_CLAUSE_LOCATION (c),
13119 "linear clause applied to non-integral non-pointer "
13120 "variable with type %qT", TREE_TYPE (t));
13121 remove = true;
13122 break;
13123 }
13124 if (TYPE_ATOMIC (TREE_TYPE (t)))
13125 {
13126 error_at (OMP_CLAUSE_LOCATION (c),
13127 "%<_Atomic%> %qD in %<linear%> clause", t);
13128 remove = true;
13129 break;
13130 }
13131 }
13132 if (ort == C_ORT_OMP_DECLARE_SIMD)
13133 {
13134 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13135 if (TREE_CODE (s) == PARM_DECL)
13136 {
13137 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13138 /* map_head bitmap is used as uniform_head if
13139 declare_simd. */
13140 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13141 linear_variable_step_check = true;
13142 goto check_dup_generic;
13143 }
13144 if (TREE_CODE (s) != INTEGER_CST)
13145 {
13146 error_at (OMP_CLAUSE_LOCATION (c),
13147 "%<linear%> clause step %qE is neither constant "
13148 "nor a parameter", s);
13149 remove = true;
13150 break;
13151 }
13152 }
13153 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13154 {
13155 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13156 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13157 OMP_CLAUSE_DECL (c), s);
13158 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13159 sizetype, fold_convert (sizetype, s),
13160 fold_convert
13161 (sizetype, OMP_CLAUSE_DECL (c)));
13162 if (s == error_mark_node)
13163 s = size_one_node;
13164 OMP_CLAUSE_LINEAR_STEP (c) = s;
13165 }
13166 else
13167 OMP_CLAUSE_LINEAR_STEP (c)
13168 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
13169 goto check_dup_generic;
13170
13171 check_dup_generic:
13172 t = OMP_CLAUSE_DECL (c);
13173 check_dup_generic_t:
13174 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13175 {
13176 error_at (OMP_CLAUSE_LOCATION (c),
13177 "%qE is not a variable in clause %qs", t,
13178 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13179 remove = true;
13180 }
13181 else if (ort == C_ORT_ACC
13182 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13183 {
13184 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13185 {
13186 error ("%qD appears more than once in reduction clauses", t);
13187 remove = true;
13188 }
13189 else
13190 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13191 }
13192 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13193 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13194 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13195 {
13196 error_at (OMP_CLAUSE_LOCATION (c),
13197 "%qE appears more than once in data clauses", t);
13198 remove = true;
13199 }
13200 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13201 && bitmap_bit_p (&map_head, DECL_UID (t)))
13202 {
13203 if (ort == C_ORT_ACC)
13204 error ("%qD appears more than once in data clauses", t);
13205 else
13206 error ("%qD appears both in data and map clauses", t);
13207 remove = true;
13208 }
13209 else
13210 bitmap_set_bit (&generic_head, DECL_UID (t));
13211 break;
13212
13213 case OMP_CLAUSE_FIRSTPRIVATE:
13214 t = OMP_CLAUSE_DECL (c);
13215 need_complete = true;
13216 need_implicitly_determined = true;
13217 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13218 {
13219 error_at (OMP_CLAUSE_LOCATION (c),
13220 "%qE is not a variable in clause %<firstprivate%>", t);
13221 remove = true;
13222 }
13223 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13224 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13225 {
13226 error_at (OMP_CLAUSE_LOCATION (c),
13227 "%qE appears more than once in data clauses", t);
13228 remove = true;
13229 }
13230 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13231 {
13232 if (ort == C_ORT_ACC)
13233 error ("%qD appears more than once in data clauses", t);
13234 else
13235 error ("%qD appears both in data and map clauses", t);
13236 remove = true;
13237 }
13238 else
13239 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13240 break;
13241
13242 case OMP_CLAUSE_LASTPRIVATE:
13243 t = OMP_CLAUSE_DECL (c);
13244 need_complete = true;
13245 need_implicitly_determined = true;
13246 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13247 {
13248 error_at (OMP_CLAUSE_LOCATION (c),
13249 "%qE is not a variable in clause %<lastprivate%>", t);
13250 remove = true;
13251 }
13252 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13253 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13254 {
13255 error_at (OMP_CLAUSE_LOCATION (c),
13256 "%qE appears more than once in data clauses", t);
13257 remove = true;
13258 }
13259 else
13260 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13261 break;
13262
13263 case OMP_CLAUSE_ALIGNED:
13264 t = OMP_CLAUSE_DECL (c);
13265 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13266 {
13267 error_at (OMP_CLAUSE_LOCATION (c),
13268 "%qE is not a variable in %<aligned%> clause", t);
13269 remove = true;
13270 }
13271 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13272 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13273 {
13274 error_at (OMP_CLAUSE_LOCATION (c),
13275 "%qE in %<aligned%> clause is neither a pointer nor "
13276 "an array", t);
13277 remove = true;
13278 }
13279 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13280 {
13281 error_at (OMP_CLAUSE_LOCATION (c),
13282 "%<_Atomic%> %qD in %<aligned%> clause", t);
13283 remove = true;
13284 break;
13285 }
13286 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13287 {
13288 error_at (OMP_CLAUSE_LOCATION (c),
13289 "%qE appears more than once in %<aligned%> clauses",
13290 t);
13291 remove = true;
13292 }
13293 else
13294 bitmap_set_bit (&aligned_head, DECL_UID (t));
13295 break;
13296
13297 case OMP_CLAUSE_DEPEND:
13298 t = OMP_CLAUSE_DECL (c);
13299 if (t == NULL_TREE)
13300 {
13301 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13302 == OMP_CLAUSE_DEPEND_SOURCE);
13303 break;
13304 }
13305 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13306 {
13307 gcc_assert (TREE_CODE (t) == TREE_LIST);
13308 for (; t; t = TREE_CHAIN (t))
13309 {
13310 tree decl = TREE_VALUE (t);
13311 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13312 {
13313 tree offset = TREE_PURPOSE (t);
13314 bool neg = wi::neg_p ((wide_int) offset);
13315 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13316 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13317 neg ? MINUS_EXPR : PLUS_EXPR,
13318 decl, offset);
13319 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13320 sizetype,
13321 fold_convert (sizetype, t2),
13322 fold_convert (sizetype, decl));
13323 if (t2 == error_mark_node)
13324 {
13325 remove = true;
13326 break;
13327 }
13328 TREE_PURPOSE (t) = t2;
13329 }
13330 }
13331 break;
13332 }
13333 if (TREE_CODE (t) == TREE_LIST)
13334 {
13335 if (handle_omp_array_sections (c, ort))
13336 remove = true;
13337 break;
13338 }
13339 if (t == error_mark_node)
13340 remove = true;
13341 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13342 {
13343 error_at (OMP_CLAUSE_LOCATION (c),
13344 "%qE is not a variable in %<depend%> clause", t);
13345 remove = true;
13346 }
13347 else if (!c_mark_addressable (t))
13348 remove = true;
13349 break;
13350
13351 case OMP_CLAUSE_MAP:
13352 case OMP_CLAUSE_TO:
13353 case OMP_CLAUSE_FROM:
13354 case OMP_CLAUSE__CACHE_:
13355 t = OMP_CLAUSE_DECL (c);
13356 if (TREE_CODE (t) == TREE_LIST)
13357 {
13358 if (handle_omp_array_sections (c, ort))
13359 remove = true;
13360 else
13361 {
13362 t = OMP_CLAUSE_DECL (c);
13363 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13364 {
13365 error_at (OMP_CLAUSE_LOCATION (c),
13366 "array section does not have mappable type "
13367 "in %qs clause",
13368 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13369 remove = true;
13370 }
13371 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13372 {
13373 error_at (OMP_CLAUSE_LOCATION (c),
13374 "%<_Atomic%> %qE in %qs clause", t,
13375 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13376 remove = true;
13377 }
13378 while (TREE_CODE (t) == ARRAY_REF)
13379 t = TREE_OPERAND (t, 0);
13380 if (TREE_CODE (t) == COMPONENT_REF
13381 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13382 {
13383 while (TREE_CODE (t) == COMPONENT_REF)
13384 t = TREE_OPERAND (t, 0);
13385 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13386 break;
13387 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13388 {
13389 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13390 error ("%qD appears more than once in motion"
13391 " clauses", t);
13392 else if (ort == C_ORT_ACC)
13393 error ("%qD appears more than once in data"
13394 " clauses", t);
13395 else
13396 error ("%qD appears more than once in map"
13397 " clauses", t);
13398 remove = true;
13399 }
13400 else
13401 {
13402 bitmap_set_bit (&map_head, DECL_UID (t));
13403 bitmap_set_bit (&map_field_head, DECL_UID (t));
13404 }
13405 }
13406 }
13407 break;
13408 }
13409 if (t == error_mark_node)
13410 {
13411 remove = true;
13412 break;
13413 }
13414 if (TREE_CODE (t) == COMPONENT_REF
13415 && (ort & C_ORT_OMP)
13416 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13417 {
13418 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13419 {
13420 error_at (OMP_CLAUSE_LOCATION (c),
13421 "bit-field %qE in %qs clause",
13422 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13423 remove = true;
13424 }
13425 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13426 {
13427 error_at (OMP_CLAUSE_LOCATION (c),
13428 "%qE does not have a mappable type in %qs clause",
13429 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13430 remove = true;
13431 }
13432 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13433 {
13434 error_at (OMP_CLAUSE_LOCATION (c),
13435 "%<_Atomic%> %qE in %qs clause", t,
13436 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13437 remove = true;
13438 }
13439 while (TREE_CODE (t) == COMPONENT_REF)
13440 {
13441 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13442 == UNION_TYPE)
13443 {
13444 error_at (OMP_CLAUSE_LOCATION (c),
13445 "%qE is a member of a union", t);
13446 remove = true;
13447 break;
13448 }
13449 t = TREE_OPERAND (t, 0);
13450 }
13451 if (remove)
13452 break;
13453 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13454 {
13455 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13456 break;
13457 }
13458 }
13459 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13460 {
13461 error_at (OMP_CLAUSE_LOCATION (c),
13462 "%qE is not a variable in %qs clause", t,
13463 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13464 remove = true;
13465 }
13466 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13467 {
13468 error_at (OMP_CLAUSE_LOCATION (c),
13469 "%qD is threadprivate variable in %qs clause", t,
13470 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13471 remove = true;
13472 }
13473 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13474 || (OMP_CLAUSE_MAP_KIND (c)
13475 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13476 && !c_mark_addressable (t))
13477 remove = true;
13478 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13479 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13480 || (OMP_CLAUSE_MAP_KIND (c)
13481 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13482 || (OMP_CLAUSE_MAP_KIND (c)
13483 == GOMP_MAP_FORCE_DEVICEPTR)))
13484 && t == OMP_CLAUSE_DECL (c)
13485 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13486 {
13487 error_at (OMP_CLAUSE_LOCATION (c),
13488 "%qD does not have a mappable type in %qs clause", t,
13489 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13490 remove = true;
13491 }
13492 else if (TREE_TYPE (t) == error_mark_node)
13493 remove = true;
13494 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13495 {
13496 error_at (OMP_CLAUSE_LOCATION (c),
13497 "%<_Atomic%> %qE in %qs clause", t,
13498 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13499 remove = true;
13500 }
13501 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13502 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13503 {
13504 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13505 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13506 {
13507 error ("%qD appears more than once in data clauses", t);
13508 remove = true;
13509 }
13510 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13511 {
13512 if (ort == C_ORT_ACC)
13513 error ("%qD appears more than once in data clauses", t);
13514 else
13515 error ("%qD appears both in data and map clauses", t);
13516 remove = true;
13517 }
13518 else
13519 bitmap_set_bit (&generic_head, DECL_UID (t));
13520 }
13521 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13522 {
13523 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13524 error ("%qD appears more than once in motion clauses", t);
13525 else if (ort == C_ORT_ACC)
13526 error ("%qD appears more than once in data clauses", t);
13527 else
13528 error ("%qD appears more than once in map clauses", t);
13529 remove = true;
13530 }
13531 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13532 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13533 {
13534 if (ort == C_ORT_ACC)
13535 error ("%qD appears more than once in data clauses", t);
13536 else
13537 error ("%qD appears both in data and map clauses", t);
13538 remove = true;
13539 }
13540 else
13541 {
13542 bitmap_set_bit (&map_head, DECL_UID (t));
13543 if (t != OMP_CLAUSE_DECL (c)
13544 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13545 bitmap_set_bit (&map_field_head, DECL_UID (t));
13546 }
13547 break;
13548
13549 case OMP_CLAUSE_TO_DECLARE:
13550 case OMP_CLAUSE_LINK:
13551 t = OMP_CLAUSE_DECL (c);
13552 if (TREE_CODE (t) == FUNCTION_DECL
13553 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13554 ;
13555 else if (!VAR_P (t))
13556 {
13557 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13558 error_at (OMP_CLAUSE_LOCATION (c),
13559 "%qE is neither a variable nor a function name in "
13560 "clause %qs", t,
13561 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13562 else
13563 error_at (OMP_CLAUSE_LOCATION (c),
13564 "%qE is not a variable in clause %qs", t,
13565 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13566 remove = true;
13567 }
13568 else if (DECL_THREAD_LOCAL_P (t))
13569 {
13570 error_at (OMP_CLAUSE_LOCATION (c),
13571 "%qD is threadprivate variable in %qs clause", t,
13572 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13573 remove = true;
13574 }
13575 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13576 {
13577 error_at (OMP_CLAUSE_LOCATION (c),
13578 "%qD does not have a mappable type in %qs clause", t,
13579 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13580 remove = true;
13581 }
13582 if (remove)
13583 break;
13584 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13585 {
13586 error_at (OMP_CLAUSE_LOCATION (c),
13587 "%qE appears more than once on the same "
13588 "%<declare target%> directive", t);
13589 remove = true;
13590 }
13591 else
13592 bitmap_set_bit (&generic_head, DECL_UID (t));
13593 break;
13594
13595 case OMP_CLAUSE_UNIFORM:
13596 t = OMP_CLAUSE_DECL (c);
13597 if (TREE_CODE (t) != PARM_DECL)
13598 {
13599 if (DECL_P (t))
13600 error_at (OMP_CLAUSE_LOCATION (c),
13601 "%qD is not an argument in %<uniform%> clause", t);
13602 else
13603 error_at (OMP_CLAUSE_LOCATION (c),
13604 "%qE is not an argument in %<uniform%> clause", t);
13605 remove = true;
13606 break;
13607 }
13608 /* map_head bitmap is used as uniform_head if declare_simd. */
13609 bitmap_set_bit (&map_head, DECL_UID (t));
13610 goto check_dup_generic;
13611
13612 case OMP_CLAUSE_IS_DEVICE_PTR:
13613 case OMP_CLAUSE_USE_DEVICE_PTR:
13614 t = OMP_CLAUSE_DECL (c);
13615 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13616 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13617 {
13618 error_at (OMP_CLAUSE_LOCATION (c),
13619 "%qs variable is neither a pointer nor an array",
13620 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13621 remove = true;
13622 }
13623 goto check_dup_generic;
13624
13625 case OMP_CLAUSE_NOWAIT:
13626 if (copyprivate_seen)
13627 {
13628 error_at (OMP_CLAUSE_LOCATION (c),
13629 "%<nowait%> clause must not be used together "
13630 "with %<copyprivate%>");
13631 remove = true;
13632 break;
13633 }
13634 nowait_clause = pc;
13635 pc = &OMP_CLAUSE_CHAIN (c);
13636 continue;
13637
13638 case OMP_CLAUSE_IF:
13639 case OMP_CLAUSE_NUM_THREADS:
13640 case OMP_CLAUSE_NUM_TEAMS:
13641 case OMP_CLAUSE_THREAD_LIMIT:
13642 case OMP_CLAUSE_DEFAULT:
13643 case OMP_CLAUSE_UNTIED:
13644 case OMP_CLAUSE_COLLAPSE:
13645 case OMP_CLAUSE_FINAL:
13646 case OMP_CLAUSE_MERGEABLE:
13647 case OMP_CLAUSE_DEVICE:
13648 case OMP_CLAUSE_DIST_SCHEDULE:
13649 case OMP_CLAUSE_PARALLEL:
13650 case OMP_CLAUSE_FOR:
13651 case OMP_CLAUSE_SECTIONS:
13652 case OMP_CLAUSE_TASKGROUP:
13653 case OMP_CLAUSE_PROC_BIND:
13654 case OMP_CLAUSE_PRIORITY:
13655 case OMP_CLAUSE_GRAINSIZE:
13656 case OMP_CLAUSE_NUM_TASKS:
13657 case OMP_CLAUSE_NOGROUP:
13658 case OMP_CLAUSE_THREADS:
13659 case OMP_CLAUSE_SIMD:
13660 case OMP_CLAUSE_HINT:
13661 case OMP_CLAUSE_DEFAULTMAP:
13662 case OMP_CLAUSE__CILK_FOR_COUNT_:
13663 case OMP_CLAUSE_NUM_GANGS:
13664 case OMP_CLAUSE_NUM_WORKERS:
13665 case OMP_CLAUSE_VECTOR_LENGTH:
13666 case OMP_CLAUSE_ASYNC:
13667 case OMP_CLAUSE_WAIT:
13668 case OMP_CLAUSE_AUTO:
13669 case OMP_CLAUSE_INDEPENDENT:
13670 case OMP_CLAUSE_SEQ:
13671 case OMP_CLAUSE_GANG:
13672 case OMP_CLAUSE_WORKER:
13673 case OMP_CLAUSE_VECTOR:
13674 case OMP_CLAUSE_TILE:
13675 pc = &OMP_CLAUSE_CHAIN (c);
13676 continue;
13677
13678 case OMP_CLAUSE_SCHEDULE:
13679 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13680 {
13681 const char *p = NULL;
13682 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13683 {
13684 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13685 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13686 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13687 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13688 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13689 default: gcc_unreachable ();
13690 }
13691 if (p)
13692 {
13693 error_at (OMP_CLAUSE_LOCATION (c),
13694 "%<nonmonotonic%> modifier specified for %qs "
13695 "schedule kind", p);
13696 OMP_CLAUSE_SCHEDULE_KIND (c)
13697 = (enum omp_clause_schedule_kind)
13698 (OMP_CLAUSE_SCHEDULE_KIND (c)
13699 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13700 }
13701 }
13702 schedule_clause = c;
13703 pc = &OMP_CLAUSE_CHAIN (c);
13704 continue;
13705
13706 case OMP_CLAUSE_ORDERED:
13707 ordered_seen = true;
13708 pc = &OMP_CLAUSE_CHAIN (c);
13709 continue;
13710
13711 case OMP_CLAUSE_SAFELEN:
13712 safelen = c;
13713 pc = &OMP_CLAUSE_CHAIN (c);
13714 continue;
13715 case OMP_CLAUSE_SIMDLEN:
13716 simdlen = c;
13717 pc = &OMP_CLAUSE_CHAIN (c);
13718 continue;
13719
13720 case OMP_CLAUSE_INBRANCH:
13721 case OMP_CLAUSE_NOTINBRANCH:
13722 if (branch_seen)
13723 {
13724 error_at (OMP_CLAUSE_LOCATION (c),
13725 "%<inbranch%> clause is incompatible with "
13726 "%<notinbranch%>");
13727 remove = true;
13728 break;
13729 }
13730 branch_seen = true;
13731 pc = &OMP_CLAUSE_CHAIN (c);
13732 continue;
13733
13734 default:
13735 gcc_unreachable ();
13736 }
13737
13738 if (!remove)
13739 {
13740 t = OMP_CLAUSE_DECL (c);
13741
13742 if (need_complete)
13743 {
13744 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13745 if (t == error_mark_node)
13746 remove = true;
13747 }
13748
13749 if (need_implicitly_determined)
13750 {
13751 const char *share_name = NULL;
13752
13753 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13754 share_name = "threadprivate";
13755 else switch (c_omp_predetermined_sharing (t))
13756 {
13757 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13758 break;
13759 case OMP_CLAUSE_DEFAULT_SHARED:
13760 /* const vars may be specified in firstprivate clause. */
13761 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13762 && TREE_READONLY (t))
13763 break;
13764 share_name = "shared";
13765 break;
13766 case OMP_CLAUSE_DEFAULT_PRIVATE:
13767 share_name = "private";
13768 break;
13769 default:
13770 gcc_unreachable ();
13771 }
13772 if (share_name)
13773 {
13774 error_at (OMP_CLAUSE_LOCATION (c),
13775 "%qE is predetermined %qs for %qs",
13776 t, share_name,
13777 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13778 remove = true;
13779 }
13780 }
13781 }
13782
13783 if (remove)
13784 *pc = OMP_CLAUSE_CHAIN (c);
13785 else
13786 pc = &OMP_CLAUSE_CHAIN (c);
13787 }
13788
13789 if (simdlen
13790 && safelen
13791 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13792 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13793 {
13794 error_at (OMP_CLAUSE_LOCATION (simdlen),
13795 "%<simdlen%> clause value is bigger than "
13796 "%<safelen%> clause value");
13797 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13798 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13799 }
13800
13801 if (ordered_seen
13802 && schedule_clause
13803 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13804 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13805 {
13806 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13807 "%<nonmonotonic%> schedule modifier specified together "
13808 "with %<ordered%> clause");
13809 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13810 = (enum omp_clause_schedule_kind)
13811 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13812 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13813 }
13814
13815 if (linear_variable_step_check)
13816 for (pc = &clauses, c = clauses; c ; c = *pc)
13817 {
13818 bool remove = false;
13819 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13820 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13821 && !bitmap_bit_p (&map_head,
13822 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13823 {
13824 error_at (OMP_CLAUSE_LOCATION (c),
13825 "%<linear%> clause step is a parameter %qD not "
13826 "specified in %<uniform%> clause",
13827 OMP_CLAUSE_LINEAR_STEP (c));
13828 remove = true;
13829 }
13830
13831 if (remove)
13832 *pc = OMP_CLAUSE_CHAIN (c);
13833 else
13834 pc = &OMP_CLAUSE_CHAIN (c);
13835 }
13836
13837 bitmap_obstack_release (NULL);
13838 return clauses;
13839 }
13840
13841 /* Return code to initialize DST with a copy constructor from SRC.
13842 C doesn't have copy constructors nor assignment operators, only for
13843 _Atomic vars we need to perform __atomic_load from src into a temporary
13844 followed by __atomic_store of the temporary to dst. */
13845
13846 tree
13847 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
13848 {
13849 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
13850 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13851
13852 location_t loc = OMP_CLAUSE_LOCATION (clause);
13853 tree type = TREE_TYPE (dst);
13854 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
13855 tree tmp = create_tmp_var (nonatomic_type);
13856 tree tmp_addr = build_fold_addr_expr (tmp);
13857 TREE_ADDRESSABLE (tmp) = 1;
13858 TREE_NO_WARNING (tmp) = 1;
13859 tree src_addr = build_fold_addr_expr (src);
13860 tree dst_addr = build_fold_addr_expr (dst);
13861 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
13862 vec<tree, va_gc> *params;
13863 /* Expansion of a generic atomic load may require an addition
13864 element, so allocate enough to prevent a resize. */
13865 vec_alloc (params, 4);
13866
13867 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
13868 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
13869 params->quick_push (src_addr);
13870 params->quick_push (tmp_addr);
13871 params->quick_push (seq_cst);
13872 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13873
13874 vec_alloc (params, 4);
13875
13876 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
13877 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
13878 params->quick_push (dst_addr);
13879 params->quick_push (tmp_addr);
13880 params->quick_push (seq_cst);
13881 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13882 return build2 (COMPOUND_EXPR, void_type_node, load, store);
13883 }
13884
13885 /* Create a transaction node. */
13886
13887 tree
13888 c_finish_transaction (location_t loc, tree block, int flags)
13889 {
13890 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13891 if (flags & TM_STMT_ATTR_OUTER)
13892 TRANSACTION_EXPR_OUTER (stmt) = 1;
13893 if (flags & TM_STMT_ATTR_RELAXED)
13894 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13895 return add_stmt (stmt);
13896 }
13897
13898 /* Make a variant type in the proper way for C/C++, propagating qualifiers
13899 down to the element type of an array. If ORIG_QUAL_TYPE is not
13900 NULL, then it should be used as the qualified type
13901 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13902 preserve information about the typedef name from which an array
13903 type was derived). */
13904
13905 tree
13906 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
13907 size_t orig_qual_indirect)
13908 {
13909 if (type == error_mark_node)
13910 return type;
13911
13912 if (TREE_CODE (type) == ARRAY_TYPE)
13913 {
13914 tree t;
13915 tree element_type = c_build_qualified_type (TREE_TYPE (type),
13916 type_quals, orig_qual_type,
13917 orig_qual_indirect - 1);
13918
13919 /* See if we already have an identically qualified type. */
13920 if (orig_qual_type && orig_qual_indirect == 0)
13921 t = orig_qual_type;
13922 else
13923 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13924 {
13925 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13926 && TYPE_NAME (t) == TYPE_NAME (type)
13927 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13928 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13929 TYPE_ATTRIBUTES (type)))
13930 break;
13931 }
13932 if (!t)
13933 {
13934 tree domain = TYPE_DOMAIN (type);
13935
13936 t = build_variant_type_copy (type);
13937 TREE_TYPE (t) = element_type;
13938
13939 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13940 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13941 SET_TYPE_STRUCTURAL_EQUALITY (t);
13942 else if (TYPE_CANONICAL (element_type) != element_type
13943 || (domain && TYPE_CANONICAL (domain) != domain))
13944 {
13945 tree unqualified_canon
13946 = build_array_type (TYPE_CANONICAL (element_type),
13947 domain? TYPE_CANONICAL (domain)
13948 : NULL_TREE);
13949 if (TYPE_REVERSE_STORAGE_ORDER (type))
13950 {
13951 unqualified_canon
13952 = build_distinct_type_copy (unqualified_canon);
13953 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13954 }
13955 TYPE_CANONICAL (t)
13956 = c_build_qualified_type (unqualified_canon, type_quals);
13957 }
13958 else
13959 TYPE_CANONICAL (t) = t;
13960 }
13961 return t;
13962 }
13963
13964 /* A restrict-qualified pointer type must be a pointer to object or
13965 incomplete type. Note that the use of POINTER_TYPE_P also allows
13966 REFERENCE_TYPEs, which is appropriate for C++. */
13967 if ((type_quals & TYPE_QUAL_RESTRICT)
13968 && (!POINTER_TYPE_P (type)
13969 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13970 {
13971 error ("invalid use of %<restrict%>");
13972 type_quals &= ~TYPE_QUAL_RESTRICT;
13973 }
13974
13975 tree var_type = (orig_qual_type && orig_qual_indirect == 0
13976 ? orig_qual_type
13977 : build_qualified_type (type, type_quals));
13978 /* A variant type does not inherit the list of incomplete vars from the
13979 type main variant. */
13980 if (RECORD_OR_UNION_TYPE_P (var_type)
13981 && TYPE_MAIN_VARIANT (var_type) != var_type)
13982 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13983 return var_type;
13984 }
13985
13986 /* Build a VA_ARG_EXPR for the C parser. */
13987
13988 tree
13989 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
13990 {
13991 if (error_operand_p (type))
13992 return error_mark_node;
13993 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13994 order because it takes the address of the expression. */
13995 else if (handled_component_p (expr)
13996 && reverse_storage_order_for_component_p (expr))
13997 {
13998 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
13999 return error_mark_node;
14000 }
14001 else if (!COMPLETE_TYPE_P (type))
14002 {
14003 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
14004 "type %qT", type);
14005 return error_mark_node;
14006 }
14007 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
14008 warning_at (loc2, OPT_Wc___compat,
14009 "C++ requires promoted type, not enum type, in %<va_arg%>");
14010 return build_va_arg (loc2, expr, type);
14011 }
14012
14013 /* Return truthvalue of whether T1 is the same tree structure as T2.
14014 Return 1 if they are the same. Return false if they are different. */
14015
14016 bool
14017 c_tree_equal (tree t1, tree t2)
14018 {
14019 enum tree_code code1, code2;
14020
14021 if (t1 == t2)
14022 return true;
14023 if (!t1 || !t2)
14024 return false;
14025
14026 for (code1 = TREE_CODE (t1);
14027 CONVERT_EXPR_CODE_P (code1)
14028 || code1 == NON_LVALUE_EXPR;
14029 code1 = TREE_CODE (t1))
14030 t1 = TREE_OPERAND (t1, 0);
14031 for (code2 = TREE_CODE (t2);
14032 CONVERT_EXPR_CODE_P (code2)
14033 || code2 == NON_LVALUE_EXPR;
14034 code2 = TREE_CODE (t2))
14035 t2 = TREE_OPERAND (t2, 0);
14036
14037 /* They might have become equal now. */
14038 if (t1 == t2)
14039 return true;
14040
14041 if (code1 != code2)
14042 return false;
14043
14044 switch (code1)
14045 {
14046 case INTEGER_CST:
14047 return wi::eq_p (t1, t2);
14048
14049 case REAL_CST:
14050 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
14051
14052 case STRING_CST:
14053 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
14054 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
14055 TREE_STRING_LENGTH (t1));
14056
14057 case FIXED_CST:
14058 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
14059 TREE_FIXED_CST (t2));
14060
14061 case COMPLEX_CST:
14062 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
14063 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
14064
14065 case VECTOR_CST:
14066 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
14067
14068 case CONSTRUCTOR:
14069 /* We need to do this when determining whether or not two
14070 non-type pointer to member function template arguments
14071 are the same. */
14072 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
14073 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
14074 return false;
14075 {
14076 tree field, value;
14077 unsigned int i;
14078 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
14079 {
14080 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
14081 if (!c_tree_equal (field, elt2->index)
14082 || !c_tree_equal (value, elt2->value))
14083 return false;
14084 }
14085 }
14086 return true;
14087
14088 case TREE_LIST:
14089 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
14090 return false;
14091 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14092 return false;
14093 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14094
14095 case SAVE_EXPR:
14096 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14097
14098 case CALL_EXPR:
14099 {
14100 tree arg1, arg2;
14101 call_expr_arg_iterator iter1, iter2;
14102 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14103 return false;
14104 for (arg1 = first_call_expr_arg (t1, &iter1),
14105 arg2 = first_call_expr_arg (t2, &iter2);
14106 arg1 && arg2;
14107 arg1 = next_call_expr_arg (&iter1),
14108 arg2 = next_call_expr_arg (&iter2))
14109 if (!c_tree_equal (arg1, arg2))
14110 return false;
14111 if (arg1 || arg2)
14112 return false;
14113 return true;
14114 }
14115
14116 case TARGET_EXPR:
14117 {
14118 tree o1 = TREE_OPERAND (t1, 0);
14119 tree o2 = TREE_OPERAND (t2, 0);
14120
14121 /* Special case: if either target is an unallocated VAR_DECL,
14122 it means that it's going to be unified with whatever the
14123 TARGET_EXPR is really supposed to initialize, so treat it
14124 as being equivalent to anything. */
14125 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
14126 && !DECL_RTL_SET_P (o1))
14127 /*Nop*/;
14128 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
14129 && !DECL_RTL_SET_P (o2))
14130 /*Nop*/;
14131 else if (!c_tree_equal (o1, o2))
14132 return false;
14133
14134 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14135 }
14136
14137 case COMPONENT_REF:
14138 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14139 return false;
14140 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14141
14142 case PARM_DECL:
14143 case VAR_DECL:
14144 case CONST_DECL:
14145 case FIELD_DECL:
14146 case FUNCTION_DECL:
14147 case IDENTIFIER_NODE:
14148 case SSA_NAME:
14149 return false;
14150
14151 case TREE_VEC:
14152 {
14153 unsigned ix;
14154 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14155 return false;
14156 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14157 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14158 TREE_VEC_ELT (t2, ix)))
14159 return false;
14160 return true;
14161 }
14162
14163 default:
14164 break;
14165 }
14166
14167 switch (TREE_CODE_CLASS (code1))
14168 {
14169 case tcc_unary:
14170 case tcc_binary:
14171 case tcc_comparison:
14172 case tcc_expression:
14173 case tcc_vl_exp:
14174 case tcc_reference:
14175 case tcc_statement:
14176 {
14177 int i, n = TREE_OPERAND_LENGTH (t1);
14178
14179 switch (code1)
14180 {
14181 case PREINCREMENT_EXPR:
14182 case PREDECREMENT_EXPR:
14183 case POSTINCREMENT_EXPR:
14184 case POSTDECREMENT_EXPR:
14185 n = 1;
14186 break;
14187 case ARRAY_REF:
14188 n = 2;
14189 break;
14190 default:
14191 break;
14192 }
14193
14194 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14195 && n != TREE_OPERAND_LENGTH (t2))
14196 return false;
14197
14198 for (i = 0; i < n; ++i)
14199 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14200 return false;
14201
14202 return true;
14203 }
14204
14205 case tcc_type:
14206 return comptypes (t1, t2);
14207 default:
14208 gcc_unreachable ();
14209 }
14210 /* We can get here with --disable-checking. */
14211 return false;
14212 }
14213
14214 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
14215 spawn-helper and BODY is the newly created body for FNDECL. */
14216
14217 void
14218 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
14219 {
14220 tree list = alloc_stmt_list ();
14221 tree frame = make_cilk_frame (fndecl);
14222 tree dtor = create_cilk_function_exit (frame, false, true);
14223 add_local_decl (cfun, frame);
14224
14225 DECL_SAVED_TREE (fndecl) = list;
14226
14227 tree body_list = alloc_stmt_list ();
14228 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
14229 body = fold_build_cleanup_point_expr (void_type_node, body);
14230
14231 append_to_statement_list (body, &body_list);
14232 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
14233 body_list, dtor), &list);
14234 }
14235
14236 /* Returns true when the function declaration FNDECL is implicit,
14237 introduced as a result of a call to an otherwise undeclared
14238 function, and false otherwise. */
14239
14240 bool
14241 c_decl_implicit (const_tree fndecl)
14242 {
14243 return C_DECL_IMPLICIT (fndecl);
14244 }