1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2018 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 "gomp-constants.h"
50 #include "spellcheck-tree.h"
51 #include "gcc-rich-location.h"
52 #include "stringpool.h"
53 #include "attribs.h"
54 #include "asan.h"
55
56 /* Possible cases of implicit bad conversions. Used to select
57 diagnostic messages in convert_for_assignment. */
58 enum impl_conv {
59 ic_argpass,
60 ic_assign,
61 ic_init,
62 ic_return
63 };
64
65 /* The level of nesting inside "__alignof__". */
66 int in_alignof;
67
68 /* The level of nesting inside "sizeof". */
69 int in_sizeof;
70
71 /* The level of nesting inside "typeof". */
72 int in_typeof;
73
74 /* The argument of last parsed sizeof expression, only to be tested
75 if expr.original_code == SIZEOF_EXPR. */
76 tree c_last_sizeof_arg;
77 location_t c_last_sizeof_loc;
78
79 /* Nonzero if we might need to print a "missing braces around
80 initializer" message within this initializer. */
81 static int found_missing_braces;
82
83 static int require_constant_value;
84 static int require_constant_elements;
85
86 static bool null_pointer_constant_p (const_tree);
87 static tree qualify_type (tree, tree);
88 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
89 bool *);
90 static int comp_target_types (location_t, tree, tree);
91 static int function_types_compatible_p (const_tree, const_tree, bool *,
92 bool *);
93 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
94 static tree lookup_field (tree, tree);
95 static int convert_arguments (location_t, vec<location_t>, tree,
96 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
97 tree);
98 static tree pointer_diff (location_t, tree, tree, tree *);
99 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
100 enum impl_conv, bool, tree, tree, int);
101 static tree valid_compound_expr_initializer (tree, tree);
102 static void push_string (const char *);
103 static void push_member_name (tree);
104 static int spelling_length (void);
105 static char *print_spelling (char *);
106 static void warning_init (location_t, int, const char *);
107 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
108 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
109 bool, struct obstack *);
110 static void output_pending_init_elements (int, struct obstack *);
111 static bool set_designator (location_t, bool, struct obstack *);
112 static void push_range_stack (tree, struct obstack *);
113 static void add_pending_init (location_t, tree, tree, tree, bool,
114 struct obstack *);
115 static void set_nonincremental_init (struct obstack *);
116 static void set_nonincremental_init_from_string (tree, struct obstack *);
117 static tree find_init_member (tree, struct obstack *);
118 static void readonly_warning (tree, enum lvalue_use);
119 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
120 static void record_maybe_used_decl (tree);
121 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
122
123 /* Return true if EXP is a null pointer constant, false otherwise. */
124
125 static bool
null_pointer_constant_p(const_tree expr)126 null_pointer_constant_p (const_tree expr)
127 {
128 /* This should really operate on c_expr structures, but they aren't
129 yet available everywhere required. */
130 tree type = TREE_TYPE (expr);
131 return (TREE_CODE (expr) == INTEGER_CST
132 && !TREE_OVERFLOW (expr)
133 && integer_zerop (expr)
134 && (INTEGRAL_TYPE_P (type)
135 || (TREE_CODE (type) == POINTER_TYPE
136 && VOID_TYPE_P (TREE_TYPE (type))
137 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
138 }
139
140 /* EXPR may appear in an unevaluated part of an integer constant
141 expression, but not in an evaluated part. Wrap it in a
142 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
143 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
144
145 static tree
note_integer_operands(tree expr)146 note_integer_operands (tree expr)
147 {
148 tree ret;
149 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
150 {
151 ret = copy_node (expr);
152 TREE_OVERFLOW (ret) = 1;
153 }
154 else
155 {
156 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
157 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
158 }
159 return ret;
160 }
161
162 /* Having checked whether EXPR may appear in an unevaluated part of an
163 integer constant expression and found that it may, remove any
164 C_MAYBE_CONST_EXPR noting this fact and return the resulting
165 expression. */
166
167 static inline tree
remove_c_maybe_const_expr(tree expr)168 remove_c_maybe_const_expr (tree expr)
169 {
170 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
171 return C_MAYBE_CONST_EXPR_EXPR (expr);
172 else
173 return expr;
174 }
175
176 /* This is a cache to hold if two types are compatible or not. */
177
178 struct tagged_tu_seen_cache {
179 const struct tagged_tu_seen_cache * next;
180 const_tree t1;
181 const_tree t2;
182 /* The return value of tagged_types_tu_compatible_p if we had seen
183 these two types already. */
184 int val;
185 };
186
187 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
188 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
189
190 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
191 does not have an incomplete type. (That includes void types.)
192 LOC is the location of the use. */
193
194 tree
require_complete_type(location_t loc,tree value)195 require_complete_type (location_t loc, tree value)
196 {
197 tree type = TREE_TYPE (value);
198
199 if (error_operand_p (value))
200 return error_mark_node;
201
202 /* First, detect a valid value with a complete type. */
203 if (COMPLETE_TYPE_P (type))
204 return value;
205
206 c_incomplete_type_error (loc, value, type);
207 return error_mark_node;
208 }
209
210 /* Print an error message for invalid use of an incomplete type.
211 VALUE is the expression that was used (or 0 if that isn't known)
212 and TYPE is the type that was invalid. LOC is the location for
213 the error. */
214
215 void
c_incomplete_type_error(location_t loc,const_tree value,const_tree type)216 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
217 {
218 /* Avoid duplicate error message. */
219 if (TREE_CODE (type) == ERROR_MARK)
220 return;
221
222 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
223 error_at (loc, "%qD has an incomplete type %qT", value, type);
224 else
225 {
226 retry:
227 /* We must print an error message. Be clever about what it says. */
228
229 switch (TREE_CODE (type))
230 {
231 case RECORD_TYPE:
232 case UNION_TYPE:
233 case ENUMERAL_TYPE:
234 break;
235
236 case VOID_TYPE:
237 error_at (loc, "invalid use of void expression");
238 return;
239
240 case ARRAY_TYPE:
241 if (TYPE_DOMAIN (type))
242 {
243 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
244 {
245 error_at (loc, "invalid use of flexible array member");
246 return;
247 }
248 type = TREE_TYPE (type);
249 goto retry;
250 }
251 error_at (loc, "invalid use of array with unspecified bounds");
252 return;
253
254 default:
255 gcc_unreachable ();
256 }
257
258 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
259 error_at (loc, "invalid use of undefined type %qT", type);
260 else
261 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
262 error_at (loc, "invalid use of incomplete typedef %qT", type);
263 }
264 }
265
266 /* Given a type, apply default promotions wrt unnamed function
267 arguments and return the new type. */
268
269 tree
c_type_promotes_to(tree type)270 c_type_promotes_to (tree type)
271 {
272 tree ret = NULL_TREE;
273
274 if (TYPE_MAIN_VARIANT (type) == float_type_node)
275 ret = double_type_node;
276 else if (c_promoting_integer_type_p (type))
277 {
278 /* Preserve unsignedness if not really getting any wider. */
279 if (TYPE_UNSIGNED (type)
280 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
281 ret = unsigned_type_node;
282 else
283 ret = integer_type_node;
284 }
285
286 if (ret != NULL_TREE)
287 return (TYPE_ATOMIC (type)
288 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
289 : ret);
290
291 return type;
292 }
293
294 /* Return true if between two named address spaces, whether there is a superset
295 named address space that encompasses both address spaces. If there is a
296 superset, return which address space is the superset. */
297
298 static bool
addr_space_superset(addr_space_t as1,addr_space_t as2,addr_space_t * common)299 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
300 {
301 if (as1 == as2)
302 {
303 *common = as1;
304 return true;
305 }
306 else if (targetm.addr_space.subset_p (as1, as2))
307 {
308 *common = as2;
309 return true;
310 }
311 else if (targetm.addr_space.subset_p (as2, as1))
312 {
313 *common = as1;
314 return true;
315 }
316 else
317 return false;
318 }
319
320 /* Return a variant of TYPE which has all the type qualifiers of LIKE
321 as well as those of TYPE. */
322
323 static tree
qualify_type(tree type,tree like)324 qualify_type (tree type, tree like)
325 {
326 addr_space_t as_type = TYPE_ADDR_SPACE (type);
327 addr_space_t as_like = TYPE_ADDR_SPACE (like);
328 addr_space_t as_common;
329
330 /* If the two named address spaces are different, determine the common
331 superset address space. If there isn't one, raise an error. */
332 if (!addr_space_superset (as_type, as_like, &as_common))
333 {
334 as_common = as_type;
335 error ("%qT and %qT are in disjoint named address spaces",
336 type, like);
337 }
338
339 return c_build_qualified_type (type,
340 TYPE_QUALS_NO_ADDR_SPACE (type)
341 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
342 | ENCODE_QUAL_ADDR_SPACE (as_common));
343 }
344
345 /* Return true iff the given tree T is a variable length array. */
346
347 bool
c_vla_type_p(const_tree t)348 c_vla_type_p (const_tree t)
349 {
350 if (TREE_CODE (t) == ARRAY_TYPE
351 && C_TYPE_VARIABLE_SIZE (t))
352 return true;
353 return false;
354 }
355
356 /* Return the composite type of two compatible types.
357
358 We assume that comptypes has already been done and returned
359 nonzero; if that isn't so, this may crash. In particular, we
360 assume that qualifiers match. */
361
362 tree
composite_type(tree t1,tree t2)363 composite_type (tree t1, tree t2)
364 {
365 enum tree_code code1;
366 enum tree_code code2;
367 tree attributes;
368
369 /* Save time if the two types are the same. */
370
371 if (t1 == t2) return t1;
372
373 /* If one type is nonsense, use the other. */
374 if (t1 == error_mark_node)
375 return t2;
376 if (t2 == error_mark_node)
377 return t1;
378
379 code1 = TREE_CODE (t1);
380 code2 = TREE_CODE (t2);
381
382 /* Merge the attributes. */
383 attributes = targetm.merge_type_attributes (t1, t2);
384
385 /* If one is an enumerated type and the other is the compatible
386 integer type, the composite type might be either of the two
387 (DR#013 question 3). For consistency, use the enumerated type as
388 the composite type. */
389
390 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
391 return t1;
392 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
393 return t2;
394
395 gcc_assert (code1 == code2);
396
397 switch (code1)
398 {
399 case POINTER_TYPE:
400 /* For two pointers, do this recursively on the target type. */
401 {
402 tree pointed_to_1 = TREE_TYPE (t1);
403 tree pointed_to_2 = TREE_TYPE (t2);
404 tree target = composite_type (pointed_to_1, pointed_to_2);
405 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
406 t1 = build_type_attribute_variant (t1, attributes);
407 return qualify_type (t1, t2);
408 }
409
410 case ARRAY_TYPE:
411 {
412 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
413 int quals;
414 tree unqual_elt;
415 tree d1 = TYPE_DOMAIN (t1);
416 tree d2 = TYPE_DOMAIN (t2);
417 bool d1_variable, d2_variable;
418 bool d1_zero, d2_zero;
419 bool t1_complete, t2_complete;
420
421 /* We should not have any type quals on arrays at all. */
422 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
423 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
424
425 t1_complete = COMPLETE_TYPE_P (t1);
426 t2_complete = COMPLETE_TYPE_P (t2);
427
428 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
429 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
430
431 d1_variable = (!d1_zero
432 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
433 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
434 d2_variable = (!d2_zero
435 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
436 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
437 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
438 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
439
440 /* Save space: see if the result is identical to one of the args. */
441 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
442 && (d2_variable || d2_zero || !d1_variable))
443 return build_type_attribute_variant (t1, attributes);
444 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
445 && (d1_variable || d1_zero || !d2_variable))
446 return build_type_attribute_variant (t2, attributes);
447
448 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
449 return build_type_attribute_variant (t1, attributes);
450 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
451 return build_type_attribute_variant (t2, attributes);
452
453 /* Merge the element types, and have a size if either arg has
454 one. We may have qualifiers on the element types. To set
455 up TYPE_MAIN_VARIANT correctly, we need to form the
456 composite of the unqualified types and add the qualifiers
457 back at the end. */
458 quals = TYPE_QUALS (strip_array_types (elt));
459 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
460 t1 = build_array_type (unqual_elt,
461 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
462 && (d2_variable
463 || d2_zero
464 || !d1_variable))
465 ? t1
466 : t2));
467 /* Ensure a composite type involving a zero-length array type
468 is a zero-length type not an incomplete type. */
469 if (d1_zero && d2_zero
470 && (t1_complete || t2_complete)
471 && !COMPLETE_TYPE_P (t1))
472 {
473 TYPE_SIZE (t1) = bitsize_zero_node;
474 TYPE_SIZE_UNIT (t1) = size_zero_node;
475 }
476 t1 = c_build_qualified_type (t1, quals);
477 return build_type_attribute_variant (t1, attributes);
478 }
479
480 case ENUMERAL_TYPE:
481 case RECORD_TYPE:
482 case UNION_TYPE:
483 if (attributes != NULL)
484 {
485 /* Try harder not to create a new aggregate type. */
486 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
487 return t1;
488 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
489 return t2;
490 }
491 return build_type_attribute_variant (t1, attributes);
492
493 case FUNCTION_TYPE:
494 /* Function types: prefer the one that specified arg types.
495 If both do, merge the arg types. Also merge the return types. */
496 {
497 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
498 tree p1 = TYPE_ARG_TYPES (t1);
499 tree p2 = TYPE_ARG_TYPES (t2);
500 int len;
501 tree newargs, n;
502 int i;
503
504 /* Save space: see if the result is identical to one of the args. */
505 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
506 return build_type_attribute_variant (t1, attributes);
507 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
508 return build_type_attribute_variant (t2, attributes);
509
510 /* Simple way if one arg fails to specify argument types. */
511 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
512 {
513 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
514 t1 = build_type_attribute_variant (t1, attributes);
515 return qualify_type (t1, t2);
516 }
517 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
518 {
519 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
520 t1 = build_type_attribute_variant (t1, attributes);
521 return qualify_type (t1, t2);
522 }
523
524 /* If both args specify argument types, we must merge the two
525 lists, argument by argument. */
526
527 for (len = 0, newargs = p1;
528 newargs && newargs != void_list_node;
529 len++, newargs = TREE_CHAIN (newargs))
530 ;
531
532 for (i = 0; i < len; i++)
533 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
534
535 n = newargs;
536
537 for (; p1 && p1 != void_list_node;
538 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
539 {
540 /* A null type means arg type is not specified.
541 Take whatever the other function type has. */
542 if (TREE_VALUE (p1) == NULL_TREE)
543 {
544 TREE_VALUE (n) = TREE_VALUE (p2);
545 goto parm_done;
546 }
547 if (TREE_VALUE (p2) == NULL_TREE)
548 {
549 TREE_VALUE (n) = TREE_VALUE (p1);
550 goto parm_done;
551 }
552
553 /* Given wait (union {union wait *u; int *i} *)
554 and wait (union wait *),
555 prefer union wait * as type of parm. */
556 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
557 && TREE_VALUE (p1) != TREE_VALUE (p2))
558 {
559 tree memb;
560 tree mv2 = TREE_VALUE (p2);
561 if (mv2 && mv2 != error_mark_node
562 && TREE_CODE (mv2) != ARRAY_TYPE)
563 mv2 = TYPE_MAIN_VARIANT (mv2);
564 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
565 memb; memb = DECL_CHAIN (memb))
566 {
567 tree mv3 = TREE_TYPE (memb);
568 if (mv3 && mv3 != error_mark_node
569 && TREE_CODE (mv3) != ARRAY_TYPE)
570 mv3 = TYPE_MAIN_VARIANT (mv3);
571 if (comptypes (mv3, mv2))
572 {
573 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
574 TREE_VALUE (p2));
575 pedwarn (input_location, OPT_Wpedantic,
576 "function types not truly compatible in ISO C");
577 goto parm_done;
578 }
579 }
580 }
581 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
582 && TREE_VALUE (p2) != TREE_VALUE (p1))
583 {
584 tree memb;
585 tree mv1 = TREE_VALUE (p1);
586 if (mv1 && mv1 != error_mark_node
587 && TREE_CODE (mv1) != ARRAY_TYPE)
588 mv1 = TYPE_MAIN_VARIANT (mv1);
589 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
590 memb; memb = DECL_CHAIN (memb))
591 {
592 tree mv3 = TREE_TYPE (memb);
593 if (mv3 && mv3 != error_mark_node
594 && TREE_CODE (mv3) != ARRAY_TYPE)
595 mv3 = TYPE_MAIN_VARIANT (mv3);
596 if (comptypes (mv3, mv1))
597 {
598 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
599 TREE_VALUE (p1));
600 pedwarn (input_location, OPT_Wpedantic,
601 "function types not truly compatible in ISO C");
602 goto parm_done;
603 }
604 }
605 }
606 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
607 parm_done: ;
608 }
609
610 t1 = build_function_type (valtype, newargs);
611 t1 = qualify_type (t1, t2);
612 }
613 /* FALLTHRU */
614
615 default:
616 return build_type_attribute_variant (t1, attributes);
617 }
618
619 }
620
621 /* Return the type of a conditional expression between pointers to
622 possibly differently qualified versions of compatible types.
623
624 We assume that comp_target_types has already been done and returned
625 nonzero; if that isn't so, this may crash. */
626
627 static tree
common_pointer_type(tree t1,tree t2)628 common_pointer_type (tree t1, tree t2)
629 {
630 tree attributes;
631 tree pointed_to_1, mv1;
632 tree pointed_to_2, mv2;
633 tree target;
634 unsigned target_quals;
635 addr_space_t as1, as2, as_common;
636 int quals1, quals2;
637
638 /* Save time if the two types are the same. */
639
640 if (t1 == t2) return t1;
641
642 /* If one type is nonsense, use the other. */
643 if (t1 == error_mark_node)
644 return t2;
645 if (t2 == error_mark_node)
646 return t1;
647
648 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
649 && TREE_CODE (t2) == POINTER_TYPE);
650
651 /* Merge the attributes. */
652 attributes = targetm.merge_type_attributes (t1, t2);
653
654 /* Find the composite type of the target types, and combine the
655 qualifiers of the two types' targets. Do not lose qualifiers on
656 array element types by taking the TYPE_MAIN_VARIANT. */
657 mv1 = pointed_to_1 = TREE_TYPE (t1);
658 mv2 = pointed_to_2 = TREE_TYPE (t2);
659 if (TREE_CODE (mv1) != ARRAY_TYPE)
660 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
661 if (TREE_CODE (mv2) != ARRAY_TYPE)
662 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
663 target = composite_type (mv1, mv2);
664
665 /* Strip array types to get correct qualifier for pointers to arrays */
666 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
667 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
668
669 /* For function types do not merge const qualifiers, but drop them
670 if used inconsistently. The middle-end uses these to mark const
671 and noreturn functions. */
672 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
673 target_quals = (quals1 & quals2);
674 else
675 target_quals = (quals1 | quals2);
676
677 /* If the two named address spaces are different, determine the common
678 superset address space. This is guaranteed to exist due to the
679 assumption that comp_target_type returned non-zero. */
680 as1 = TYPE_ADDR_SPACE (pointed_to_1);
681 as2 = TYPE_ADDR_SPACE (pointed_to_2);
682 if (!addr_space_superset (as1, as2, &as_common))
683 gcc_unreachable ();
684
685 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
686
687 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
688 return build_type_attribute_variant (t1, attributes);
689 }
690
691 /* Return the common type for two arithmetic types under the usual
692 arithmetic conversions. The default conversions have already been
693 applied, and enumerated types converted to their compatible integer
694 types. The resulting type is unqualified and has no attributes.
695
696 This is the type for the result of most arithmetic operations
697 if the operands have the given two types. */
698
699 static tree
c_common_type(tree t1,tree t2)700 c_common_type (tree t1, tree t2)
701 {
702 enum tree_code code1;
703 enum tree_code code2;
704
705 /* If one type is nonsense, use the other. */
706 if (t1 == error_mark_node)
707 return t2;
708 if (t2 == error_mark_node)
709 return t1;
710
711 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
712 t1 = TYPE_MAIN_VARIANT (t1);
713
714 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
715 t2 = TYPE_MAIN_VARIANT (t2);
716
717 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
718 t1 = build_type_attribute_variant (t1, NULL_TREE);
719
720 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
721 t2 = build_type_attribute_variant (t2, NULL_TREE);
722
723 /* Save time if the two types are the same. */
724
725 if (t1 == t2) return t1;
726
727 code1 = TREE_CODE (t1);
728 code2 = TREE_CODE (t2);
729
730 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
731 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
732 || code1 == INTEGER_TYPE);
733 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
734 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
735 || code2 == INTEGER_TYPE);
736
737 /* When one operand is a decimal float type, the other operand cannot be
738 a generic float type or a complex type. We also disallow vector types
739 here. */
740 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
741 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
742 {
743 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
744 {
745 error ("can%'t mix operands of decimal float and vector types");
746 return error_mark_node;
747 }
748 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
749 {
750 error ("can%'t mix operands of decimal float and complex types");
751 return error_mark_node;
752 }
753 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
754 {
755 error ("can%'t mix operands of decimal float and other float types");
756 return error_mark_node;
757 }
758 }
759
760 /* If one type is a vector type, return that type. (How the usual
761 arithmetic conversions apply to the vector types extension is not
762 precisely specified.) */
763 if (code1 == VECTOR_TYPE)
764 return t1;
765
766 if (code2 == VECTOR_TYPE)
767 return t2;
768
769 /* If one type is complex, form the common type of the non-complex
770 components, then make that complex. Use T1 or T2 if it is the
771 required type. */
772 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
773 {
774 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
775 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
776 tree subtype = c_common_type (subtype1, subtype2);
777
778 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
779 return t1;
780 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
781 return t2;
782 else
783 return build_complex_type (subtype);
784 }
785
786 /* If only one is real, use it as the result. */
787
788 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
789 return t1;
790
791 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
792 return t2;
793
794 /* If both are real and either are decimal floating point types, use
795 the decimal floating point type with the greater precision. */
796
797 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
798 {
799 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
800 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
801 return dfloat128_type_node;
802 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
803 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
804 return dfloat64_type_node;
805 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
806 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
807 return dfloat32_type_node;
808 }
809
810 /* Deal with fixed-point types. */
811 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
812 {
813 unsigned int unsignedp = 0, satp = 0;
814 scalar_mode m1, m2;
815 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
816
817 m1 = SCALAR_TYPE_MODE (t1);
818 m2 = SCALAR_TYPE_MODE (t2);
819
820 /* If one input type is saturating, the result type is saturating. */
821 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
822 satp = 1;
823
824 /* If both fixed-point types are unsigned, the result type is unsigned.
825 When mixing fixed-point and integer types, follow the sign of the
826 fixed-point type.
827 Otherwise, the result type is signed. */
828 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
829 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
830 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
831 && TYPE_UNSIGNED (t1))
832 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
833 && TYPE_UNSIGNED (t2)))
834 unsignedp = 1;
835
836 /* The result type is signed. */
837 if (unsignedp == 0)
838 {
839 /* If the input type is unsigned, we need to convert to the
840 signed type. */
841 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
842 {
843 enum mode_class mclass = (enum mode_class) 0;
844 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
845 mclass = MODE_FRACT;
846 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
847 mclass = MODE_ACCUM;
848 else
849 gcc_unreachable ();
850 m1 = as_a <scalar_mode>
851 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
852 }
853 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
854 {
855 enum mode_class mclass = (enum mode_class) 0;
856 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
857 mclass = MODE_FRACT;
858 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
859 mclass = MODE_ACCUM;
860 else
861 gcc_unreachable ();
862 m2 = as_a <scalar_mode>
863 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
864 }
865 }
866
867 if (code1 == FIXED_POINT_TYPE)
868 {
869 fbit1 = GET_MODE_FBIT (m1);
870 ibit1 = GET_MODE_IBIT (m1);
871 }
872 else
873 {
874 fbit1 = 0;
875 /* Signed integers need to subtract one sign bit. */
876 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
877 }
878
879 if (code2 == FIXED_POINT_TYPE)
880 {
881 fbit2 = GET_MODE_FBIT (m2);
882 ibit2 = GET_MODE_IBIT (m2);
883 }
884 else
885 {
886 fbit2 = 0;
887 /* Signed integers need to subtract one sign bit. */
888 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
889 }
890
891 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
892 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
893 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
894 satp);
895 }
896
897 /* Both real or both integers; use the one with greater precision. */
898
899 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
900 return t1;
901 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
902 return t2;
903
904 /* Same precision. Prefer long longs to longs to ints when the
905 same precision, following the C99 rules on integer type rank
906 (which are equivalent to the C90 rules for C90 types). */
907
908 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
909 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
910 return long_long_unsigned_type_node;
911
912 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
914 {
915 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
916 return long_long_unsigned_type_node;
917 else
918 return long_long_integer_type_node;
919 }
920
921 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
922 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
923 return long_unsigned_type_node;
924
925 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
926 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
927 {
928 /* But preserve unsignedness from the other type,
929 since long cannot hold all the values of an unsigned int. */
930 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
931 return long_unsigned_type_node;
932 else
933 return long_integer_type_node;
934 }
935
936 /* For floating types of the same TYPE_PRECISION (which we here
937 assume means either the same set of values, or sets of values
938 neither a subset of the other, with behavior being undefined in
939 the latter case), follow the rules from TS 18661-3: prefer
940 interchange types _FloatN, then standard types long double,
941 double, float, then extended types _FloatNx. For extended types,
942 check them starting with _Float128x as that seems most consistent
943 in spirit with preferring long double to double; for interchange
944 types, also check in that order for consistency although it's not
945 possible for more than one of them to have the same
946 precision. */
947 tree mv1 = TYPE_MAIN_VARIANT (t1);
948 tree mv2 = TYPE_MAIN_VARIANT (t2);
949
950 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
951 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
952 return FLOATN_TYPE_NODE (i);
953
954 /* Likewise, prefer long double to double even if same size. */
955 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
956 return long_double_type_node;
957
958 /* Likewise, prefer double to float even if same size.
959 We got a couple of embedded targets with 32 bit doubles, and the
960 pdp11 might have 64 bit floats. */
961 if (mv1 == double_type_node || mv2 == double_type_node)
962 return double_type_node;
963
964 if (mv1 == float_type_node || mv2 == float_type_node)
965 return float_type_node;
966
967 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
968 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
969 return FLOATNX_TYPE_NODE (i);
970
971 /* Otherwise prefer the unsigned one. */
972
973 if (TYPE_UNSIGNED (t1))
974 return t1;
975 else
976 return t2;
977 }
978
979 /* Wrapper around c_common_type that is used by c-common.c and other
980 front end optimizations that remove promotions. ENUMERAL_TYPEs
981 are allowed here and are converted to their compatible integer types.
982 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
983 preferably a non-Boolean type as the common type. */
984 tree
common_type(tree t1,tree t2)985 common_type (tree t1, tree t2)
986 {
987 if (TREE_CODE (t1) == ENUMERAL_TYPE)
988 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
989 if (TREE_CODE (t2) == ENUMERAL_TYPE)
990 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
991
992 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
993 if (TREE_CODE (t1) == BOOLEAN_TYPE
994 && TREE_CODE (t2) == BOOLEAN_TYPE)
995 return boolean_type_node;
996
997 /* If either type is BOOLEAN_TYPE, then return the other. */
998 if (TREE_CODE (t1) == BOOLEAN_TYPE)
999 return t2;
1000 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1001 return t1;
1002
1003 return c_common_type (t1, t2);
1004 }
1005
1006 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1007 or various other operations. Return 2 if they are compatible
1008 but a warning may be needed if you use them together. */
1009
1010 int
comptypes(tree type1,tree type2)1011 comptypes (tree type1, tree type2)
1012 {
1013 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1014 int val;
1015
1016 val = comptypes_internal (type1, type2, NULL, NULL);
1017 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1018
1019 return val;
1020 }
1021
1022 /* Like comptypes, but if it returns non-zero because enum and int are
1023 compatible, it sets *ENUM_AND_INT_P to true. */
1024
1025 static int
comptypes_check_enum_int(tree type1,tree type2,bool * enum_and_int_p)1026 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1027 {
1028 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1029 int val;
1030
1031 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1032 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1033
1034 return val;
1035 }
1036
1037 /* Like comptypes, but if it returns nonzero for different types, it
1038 sets *DIFFERENT_TYPES_P to true. */
1039
1040 int
comptypes_check_different_types(tree type1,tree type2,bool * different_types_p)1041 comptypes_check_different_types (tree type1, tree type2,
1042 bool *different_types_p)
1043 {
1044 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1045 int val;
1046
1047 val = comptypes_internal (type1, type2, NULL, different_types_p);
1048 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1049
1050 return val;
1051 }
1052
1053 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1054 or various other operations. Return 2 if they are compatible
1055 but a warning may be needed if you use them together. If
1056 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1057 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1058 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1059 NULL, and the types are compatible but different enough not to be
1060 permitted in C11 typedef redeclarations, then this sets
1061 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1062 false, but may or may not be set if the types are incompatible.
1063 This differs from comptypes, in that we don't free the seen
1064 types. */
1065
1066 static int
comptypes_internal(const_tree type1,const_tree type2,bool * enum_and_int_p,bool * different_types_p)1067 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1068 bool *different_types_p)
1069 {
1070 const_tree t1 = type1;
1071 const_tree t2 = type2;
1072 int attrval, val;
1073
1074 /* Suppress errors caused by previously reported errors. */
1075
1076 if (t1 == t2 || !t1 || !t2
1077 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1078 return 1;
1079
1080 /* Enumerated types are compatible with integer types, but this is
1081 not transitive: two enumerated types in the same translation unit
1082 are compatible with each other only if they are the same type. */
1083
1084 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1085 {
1086 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1087 if (TREE_CODE (t2) != VOID_TYPE)
1088 {
1089 if (enum_and_int_p != NULL)
1090 *enum_and_int_p = true;
1091 if (different_types_p != NULL)
1092 *different_types_p = true;
1093 }
1094 }
1095 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1096 {
1097 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1098 if (TREE_CODE (t1) != VOID_TYPE)
1099 {
1100 if (enum_and_int_p != NULL)
1101 *enum_and_int_p = true;
1102 if (different_types_p != NULL)
1103 *different_types_p = true;
1104 }
1105 }
1106
1107 if (t1 == t2)
1108 return 1;
1109
1110 /* Different classes of types can't be compatible. */
1111
1112 if (TREE_CODE (t1) != TREE_CODE (t2))
1113 return 0;
1114
1115 /* Qualifiers must match. C99 6.7.3p9 */
1116
1117 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1118 return 0;
1119
1120 /* Allow for two different type nodes which have essentially the same
1121 definition. Note that we already checked for equality of the type
1122 qualifiers (just above). */
1123
1124 if (TREE_CODE (t1) != ARRAY_TYPE
1125 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1126 return 1;
1127
1128 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1129 if (!(attrval = comp_type_attributes (t1, t2)))
1130 return 0;
1131
1132 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1133 val = 0;
1134
1135 switch (TREE_CODE (t1))
1136 {
1137 case INTEGER_TYPE:
1138 case FIXED_POINT_TYPE:
1139 case REAL_TYPE:
1140 /* With these nodes, we can't determine type equivalence by
1141 looking at what is stored in the nodes themselves, because
1142 two nodes might have different TYPE_MAIN_VARIANTs but still
1143 represent the same type. For example, wchar_t and int could
1144 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1145 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1146 and are distinct types. On the other hand, int and the
1147 following typedef
1148
1149 typedef int INT __attribute((may_alias));
1150
1151 have identical properties, different TYPE_MAIN_VARIANTs, but
1152 represent the same type. The canonical type system keeps
1153 track of equivalence in this case, so we fall back on it. */
1154 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1155
1156 case POINTER_TYPE:
1157 /* Do not remove mode information. */
1158 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1159 break;
1160 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1161 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1162 enum_and_int_p, different_types_p));
1163 break;
1164
1165 case FUNCTION_TYPE:
1166 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1167 different_types_p);
1168 break;
1169
1170 case ARRAY_TYPE:
1171 {
1172 tree d1 = TYPE_DOMAIN (t1);
1173 tree d2 = TYPE_DOMAIN (t2);
1174 bool d1_variable, d2_variable;
1175 bool d1_zero, d2_zero;
1176 val = 1;
1177
1178 /* Target types must match incl. qualifiers. */
1179 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1180 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1181 enum_and_int_p,
1182 different_types_p)) == 0)
1183 return 0;
1184
1185 if (different_types_p != NULL
1186 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1187 *different_types_p = true;
1188 /* Sizes must match unless one is missing or variable. */
1189 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1190 break;
1191
1192 d1_zero = !TYPE_MAX_VALUE (d1);
1193 d2_zero = !TYPE_MAX_VALUE (d2);
1194
1195 d1_variable = (!d1_zero
1196 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1197 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1198 d2_variable = (!d2_zero
1199 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1200 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1201 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1202 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1203
1204 if (different_types_p != NULL
1205 && d1_variable != d2_variable)
1206 *different_types_p = true;
1207 if (d1_variable || d2_variable)
1208 break;
1209 if (d1_zero && d2_zero)
1210 break;
1211 if (d1_zero || d2_zero
1212 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1213 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1214 val = 0;
1215
1216 break;
1217 }
1218
1219 case ENUMERAL_TYPE:
1220 case RECORD_TYPE:
1221 case UNION_TYPE:
1222 if (val != 1 && !same_translation_unit_p (t1, t2))
1223 {
1224 tree a1 = TYPE_ATTRIBUTES (t1);
1225 tree a2 = TYPE_ATTRIBUTES (t2);
1226
1227 if (! attribute_list_contained (a1, a2)
1228 && ! attribute_list_contained (a2, a1))
1229 break;
1230
1231 if (attrval != 2)
1232 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1233 different_types_p);
1234 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1235 different_types_p);
1236 }
1237 break;
1238
1239 case VECTOR_TYPE:
1240 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1241 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1242 enum_and_int_p, different_types_p));
1243 break;
1244
1245 default:
1246 break;
1247 }
1248 return attrval == 2 && val == 1 ? 2 : val;
1249 }
1250
1251 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1252 their qualifiers, except for named address spaces. If the pointers point to
1253 different named addresses, then we must determine if one address space is a
1254 subset of the other. */
1255
1256 static int
comp_target_types(location_t location,tree ttl,tree ttr)1257 comp_target_types (location_t location, tree ttl, tree ttr)
1258 {
1259 int val;
1260 int val_ped;
1261 tree mvl = TREE_TYPE (ttl);
1262 tree mvr = TREE_TYPE (ttr);
1263 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1264 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1265 addr_space_t as_common;
1266 bool enum_and_int_p;
1267
1268 /* Fail if pointers point to incompatible address spaces. */
1269 if (!addr_space_superset (asl, asr, &as_common))
1270 return 0;
1271
1272 /* For pedantic record result of comptypes on arrays before losing
1273 qualifiers on the element type below. */
1274 val_ped = 1;
1275
1276 if (TREE_CODE (mvl) == ARRAY_TYPE
1277 && TREE_CODE (mvr) == ARRAY_TYPE)
1278 val_ped = comptypes (mvl, mvr);
1279
1280 /* Qualifiers on element types of array types that are
1281 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1282
1283 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1284 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1285 : TYPE_MAIN_VARIANT (mvl));
1286
1287 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1288 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1289 : TYPE_MAIN_VARIANT (mvr));
1290
1291 enum_and_int_p = false;
1292 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1293
1294 if (val == 1 && val_ped != 1)
1295 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1296 "are incompatible in ISO C");
1297
1298 if (val == 2)
1299 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1300
1301 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1302 warning_at (location, OPT_Wc___compat,
1303 "pointer target types incompatible in C++");
1304
1305 return val;
1306 }
1307
1308 /* Subroutines of `comptypes'. */
1309
1310 /* Determine whether two trees derive from the same translation unit.
1311 If the CONTEXT chain ends in a null, that tree's context is still
1312 being parsed, so if two trees have context chains ending in null,
1313 they're in the same translation unit. */
1314
1315 bool
same_translation_unit_p(const_tree t1,const_tree t2)1316 same_translation_unit_p (const_tree t1, const_tree t2)
1317 {
1318 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1319 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1320 {
1321 case tcc_declaration:
1322 t1 = DECL_CONTEXT (t1); break;
1323 case tcc_type:
1324 t1 = TYPE_CONTEXT (t1); break;
1325 case tcc_exceptional:
1326 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1327 default: gcc_unreachable ();
1328 }
1329
1330 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1331 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1332 {
1333 case tcc_declaration:
1334 t2 = DECL_CONTEXT (t2); break;
1335 case tcc_type:
1336 t2 = TYPE_CONTEXT (t2); break;
1337 case tcc_exceptional:
1338 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1339 default: gcc_unreachable ();
1340 }
1341
1342 return t1 == t2;
1343 }
1344
1345 /* Allocate the seen two types, assuming that they are compatible. */
1346
1347 static struct tagged_tu_seen_cache *
alloc_tagged_tu_seen_cache(const_tree t1,const_tree t2)1348 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1349 {
1350 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1351 tu->next = tagged_tu_seen_base;
1352 tu->t1 = t1;
1353 tu->t2 = t2;
1354
1355 tagged_tu_seen_base = tu;
1356
1357 /* The C standard says that two structures in different translation
1358 units are compatible with each other only if the types of their
1359 fields are compatible (among other things). We assume that they
1360 are compatible until proven otherwise when building the cache.
1361 An example where this can occur is:
1362 struct a
1363 {
1364 struct a *next;
1365 };
1366 If we are comparing this against a similar struct in another TU,
1367 and did not assume they were compatible, we end up with an infinite
1368 loop. */
1369 tu->val = 1;
1370 return tu;
1371 }
1372
1373 /* Free the seen types until we get to TU_TIL. */
1374
1375 static void
free_all_tagged_tu_seen_up_to(const struct tagged_tu_seen_cache * tu_til)1376 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1377 {
1378 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1379 while (tu != tu_til)
1380 {
1381 const struct tagged_tu_seen_cache *const tu1
1382 = (const struct tagged_tu_seen_cache *) tu;
1383 tu = tu1->next;
1384 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1385 }
1386 tagged_tu_seen_base = tu_til;
1387 }
1388
1389 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1390 compatible. If the two types are not the same (which has been
1391 checked earlier), this can only happen when multiple translation
1392 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1393 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1394 comptypes_internal. */
1395
1396 static int
tagged_types_tu_compatible_p(const_tree t1,const_tree t2,bool * enum_and_int_p,bool * different_types_p)1397 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1398 bool *enum_and_int_p, bool *different_types_p)
1399 {
1400 tree s1, s2;
1401 bool needs_warning = false;
1402
1403 /* We have to verify that the tags of the types are the same. This
1404 is harder than it looks because this may be a typedef, so we have
1405 to go look at the original type. It may even be a typedef of a
1406 typedef...
1407 In the case of compiler-created builtin structs the TYPE_DECL
1408 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1409 while (TYPE_NAME (t1)
1410 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1411 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1412 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1413
1414 while (TYPE_NAME (t2)
1415 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1416 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1417 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1418
1419 /* C90 didn't have the requirement that the two tags be the same. */
1420 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1421 return 0;
1422
1423 /* C90 didn't say what happened if one or both of the types were
1424 incomplete; we choose to follow C99 rules here, which is that they
1425 are compatible. */
1426 if (TYPE_SIZE (t1) == NULL
1427 || TYPE_SIZE (t2) == NULL)
1428 return 1;
1429
1430 {
1431 const struct tagged_tu_seen_cache * tts_i;
1432 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1433 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1434 return tts_i->val;
1435 }
1436
1437 switch (TREE_CODE (t1))
1438 {
1439 case ENUMERAL_TYPE:
1440 {
1441 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1442 /* Speed up the case where the type values are in the same order. */
1443 tree tv1 = TYPE_VALUES (t1);
1444 tree tv2 = TYPE_VALUES (t2);
1445
1446 if (tv1 == tv2)
1447 {
1448 return 1;
1449 }
1450
1451 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1452 {
1453 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1454 break;
1455 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1456 {
1457 tu->val = 0;
1458 return 0;
1459 }
1460 }
1461
1462 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1463 {
1464 return 1;
1465 }
1466 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1467 {
1468 tu->val = 0;
1469 return 0;
1470 }
1471
1472 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1473 {
1474 tu->val = 0;
1475 return 0;
1476 }
1477
1478 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1479 {
1480 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1481 if (s2 == NULL
1482 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1483 {
1484 tu->val = 0;
1485 return 0;
1486 }
1487 }
1488 return 1;
1489 }
1490
1491 case UNION_TYPE:
1492 {
1493 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1494 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1495 {
1496 tu->val = 0;
1497 return 0;
1498 }
1499
1500 /* Speed up the common case where the fields are in the same order. */
1501 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1502 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1503 {
1504 int result;
1505
1506 if (DECL_NAME (s1) != DECL_NAME (s2))
1507 break;
1508 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1509 enum_and_int_p, different_types_p);
1510
1511 if (result != 1 && !DECL_NAME (s1))
1512 break;
1513 if (result == 0)
1514 {
1515 tu->val = 0;
1516 return 0;
1517 }
1518 if (result == 2)
1519 needs_warning = true;
1520
1521 if (TREE_CODE (s1) == FIELD_DECL
1522 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1523 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1524 {
1525 tu->val = 0;
1526 return 0;
1527 }
1528 }
1529 if (!s1 && !s2)
1530 {
1531 tu->val = needs_warning ? 2 : 1;
1532 return tu->val;
1533 }
1534
1535 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1536 {
1537 bool ok = false;
1538
1539 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1540 if (DECL_NAME (s1) == DECL_NAME (s2))
1541 {
1542 int result;
1543
1544 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1545 enum_and_int_p,
1546 different_types_p);
1547
1548 if (result != 1 && !DECL_NAME (s1))
1549 continue;
1550 if (result == 0)
1551 {
1552 tu->val = 0;
1553 return 0;
1554 }
1555 if (result == 2)
1556 needs_warning = true;
1557
1558 if (TREE_CODE (s1) == FIELD_DECL
1559 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1560 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1561 break;
1562
1563 ok = true;
1564 break;
1565 }
1566 if (!ok)
1567 {
1568 tu->val = 0;
1569 return 0;
1570 }
1571 }
1572 tu->val = needs_warning ? 2 : 10;
1573 return tu->val;
1574 }
1575
1576 case RECORD_TYPE:
1577 {
1578 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1579
1580 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1581 s1 && s2;
1582 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1583 {
1584 int result;
1585 if (TREE_CODE (s1) != TREE_CODE (s2)
1586 || DECL_NAME (s1) != DECL_NAME (s2))
1587 break;
1588 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1589 enum_and_int_p, different_types_p);
1590 if (result == 0)
1591 break;
1592 if (result == 2)
1593 needs_warning = true;
1594
1595 if (TREE_CODE (s1) == FIELD_DECL
1596 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1597 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1598 break;
1599 }
1600 if (s1 && s2)
1601 tu->val = 0;
1602 else
1603 tu->val = needs_warning ? 2 : 1;
1604 return tu->val;
1605 }
1606
1607 default:
1608 gcc_unreachable ();
1609 }
1610 }
1611
1612 /* Return 1 if two function types F1 and F2 are compatible.
1613 If either type specifies no argument types,
1614 the other must specify a fixed number of self-promoting arg types.
1615 Otherwise, if one type specifies only the number of arguments,
1616 the other must specify that number of self-promoting arg types.
1617 Otherwise, the argument types must match.
1618 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1619
1620 static int
function_types_compatible_p(const_tree f1,const_tree f2,bool * enum_and_int_p,bool * different_types_p)1621 function_types_compatible_p (const_tree f1, const_tree f2,
1622 bool *enum_and_int_p, bool *different_types_p)
1623 {
1624 tree args1, args2;
1625 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1626 int val = 1;
1627 int val1;
1628 tree ret1, ret2;
1629
1630 ret1 = TREE_TYPE (f1);
1631 ret2 = TREE_TYPE (f2);
1632
1633 /* 'volatile' qualifiers on a function's return type used to mean
1634 the function is noreturn. */
1635 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1636 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1637 if (TYPE_VOLATILE (ret1))
1638 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1639 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1640 if (TYPE_VOLATILE (ret2))
1641 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1642 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1643 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1644 if (val == 0)
1645 return 0;
1646
1647 args1 = TYPE_ARG_TYPES (f1);
1648 args2 = TYPE_ARG_TYPES (f2);
1649
1650 if (different_types_p != NULL
1651 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1652 *different_types_p = true;
1653
1654 /* An unspecified parmlist matches any specified parmlist
1655 whose argument types don't need default promotions. */
1656
1657 if (args1 == NULL_TREE)
1658 {
1659 if (!self_promoting_args_p (args2))
1660 return 0;
1661 /* If one of these types comes from a non-prototype fn definition,
1662 compare that with the other type's arglist.
1663 If they don't match, ask for a warning (but no error). */
1664 if (TYPE_ACTUAL_ARG_TYPES (f1)
1665 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1666 enum_and_int_p, different_types_p) != 1)
1667 val = 2;
1668 return val;
1669 }
1670 if (args2 == NULL_TREE)
1671 {
1672 if (!self_promoting_args_p (args1))
1673 return 0;
1674 if (TYPE_ACTUAL_ARG_TYPES (f2)
1675 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1676 enum_and_int_p, different_types_p) != 1)
1677 val = 2;
1678 return val;
1679 }
1680
1681 /* Both types have argument lists: compare them and propagate results. */
1682 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1683 different_types_p);
1684 return val1 != 1 ? val1 : val;
1685 }
1686
1687 /* Check two lists of types for compatibility, returning 0 for
1688 incompatible, 1 for compatible, or 2 for compatible with
1689 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1690 comptypes_internal. */
1691
1692 static int
type_lists_compatible_p(const_tree args1,const_tree args2,bool * enum_and_int_p,bool * different_types_p)1693 type_lists_compatible_p (const_tree args1, const_tree args2,
1694 bool *enum_and_int_p, bool *different_types_p)
1695 {
1696 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1697 int val = 1;
1698 int newval = 0;
1699
1700 while (1)
1701 {
1702 tree a1, mv1, a2, mv2;
1703 if (args1 == NULL_TREE && args2 == NULL_TREE)
1704 return val;
1705 /* If one list is shorter than the other,
1706 they fail to match. */
1707 if (args1 == NULL_TREE || args2 == NULL_TREE)
1708 return 0;
1709 mv1 = a1 = TREE_VALUE (args1);
1710 mv2 = a2 = TREE_VALUE (args2);
1711 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1712 mv1 = (TYPE_ATOMIC (mv1)
1713 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1714 TYPE_QUAL_ATOMIC)
1715 : TYPE_MAIN_VARIANT (mv1));
1716 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1717 mv2 = (TYPE_ATOMIC (mv2)
1718 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1719 TYPE_QUAL_ATOMIC)
1720 : TYPE_MAIN_VARIANT (mv2));
1721 /* A null pointer instead of a type
1722 means there is supposed to be an argument
1723 but nothing is specified about what type it has.
1724 So match anything that self-promotes. */
1725 if (different_types_p != NULL
1726 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1727 *different_types_p = true;
1728 if (a1 == NULL_TREE)
1729 {
1730 if (c_type_promotes_to (a2) != a2)
1731 return 0;
1732 }
1733 else if (a2 == NULL_TREE)
1734 {
1735 if (c_type_promotes_to (a1) != a1)
1736 return 0;
1737 }
1738 /* If one of the lists has an error marker, ignore this arg. */
1739 else if (TREE_CODE (a1) == ERROR_MARK
1740 || TREE_CODE (a2) == ERROR_MARK)
1741 ;
1742 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1743 different_types_p)))
1744 {
1745 if (different_types_p != NULL)
1746 *different_types_p = true;
1747 /* Allow wait (union {union wait *u; int *i} *)
1748 and wait (union wait *) to be compatible. */
1749 if (TREE_CODE (a1) == UNION_TYPE
1750 && (TYPE_NAME (a1) == NULL_TREE
1751 || TYPE_TRANSPARENT_AGGR (a1))
1752 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1753 && tree_int_cst_equal (TYPE_SIZE (a1),
1754 TYPE_SIZE (a2)))
1755 {
1756 tree memb;
1757 for (memb = TYPE_FIELDS (a1);
1758 memb; memb = DECL_CHAIN (memb))
1759 {
1760 tree mv3 = TREE_TYPE (memb);
1761 if (mv3 && mv3 != error_mark_node
1762 && TREE_CODE (mv3) != ARRAY_TYPE)
1763 mv3 = (TYPE_ATOMIC (mv3)
1764 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1765 TYPE_QUAL_ATOMIC)
1766 : TYPE_MAIN_VARIANT (mv3));
1767 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1768 different_types_p))
1769 break;
1770 }
1771 if (memb == NULL_TREE)
1772 return 0;
1773 }
1774 else if (TREE_CODE (a2) == UNION_TYPE
1775 && (TYPE_NAME (a2) == NULL_TREE
1776 || TYPE_TRANSPARENT_AGGR (a2))
1777 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1778 && tree_int_cst_equal (TYPE_SIZE (a2),
1779 TYPE_SIZE (a1)))
1780 {
1781 tree memb;
1782 for (memb = TYPE_FIELDS (a2);
1783 memb; memb = DECL_CHAIN (memb))
1784 {
1785 tree mv3 = TREE_TYPE (memb);
1786 if (mv3 && mv3 != error_mark_node
1787 && TREE_CODE (mv3) != ARRAY_TYPE)
1788 mv3 = (TYPE_ATOMIC (mv3)
1789 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1790 TYPE_QUAL_ATOMIC)
1791 : TYPE_MAIN_VARIANT (mv3));
1792 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1793 different_types_p))
1794 break;
1795 }
1796 if (memb == NULL_TREE)
1797 return 0;
1798 }
1799 else
1800 return 0;
1801 }
1802
1803 /* comptypes said ok, but record if it said to warn. */
1804 if (newval > val)
1805 val = newval;
1806
1807 args1 = TREE_CHAIN (args1);
1808 args2 = TREE_CHAIN (args2);
1809 }
1810 }
1811
1812 /* Compute the size to increment a pointer by. When a function type or void
1813 type or incomplete type is passed, size_one_node is returned.
1814 This function does not emit any diagnostics; the caller is responsible
1815 for that. */
1816
1817 static tree
c_size_in_bytes(const_tree type)1818 c_size_in_bytes (const_tree type)
1819 {
1820 enum tree_code code = TREE_CODE (type);
1821
1822 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1823 || !COMPLETE_TYPE_P (type))
1824 return size_one_node;
1825
1826 /* Convert in case a char is more than one unit. */
1827 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1828 size_int (TYPE_PRECISION (char_type_node)
1829 / BITS_PER_UNIT));
1830 }
1831
1832 /* Return either DECL or its known constant value (if it has one). */
1833
1834 tree
decl_constant_value_1(tree decl,bool in_init)1835 decl_constant_value_1 (tree decl, bool in_init)
1836 {
1837 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1838 TREE_CODE (decl) != PARM_DECL
1839 && !TREE_THIS_VOLATILE (decl)
1840 && TREE_READONLY (decl)
1841 && DECL_INITIAL (decl) != NULL_TREE
1842 && !error_operand_p (DECL_INITIAL (decl))
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 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1849 return DECL_INITIAL (decl);
1850 return decl;
1851 }
1852
1853 /* Return either DECL or its known constant value (if it has one).
1854 Like the above, but always return decl outside of functions. */
1855
1856 tree
decl_constant_value(tree decl)1857 decl_constant_value (tree decl)
1858 {
1859 /* Don't change a variable array bound or initial value to a constant
1860 in a place where a variable is invalid. */
1861 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1862 }
1863
1864 /* Convert the array expression EXP to a pointer. */
1865 static tree
array_to_pointer_conversion(location_t loc,tree exp)1866 array_to_pointer_conversion (location_t loc, tree exp)
1867 {
1868 tree orig_exp = exp;
1869 tree type = TREE_TYPE (exp);
1870 tree adr;
1871 tree restype = TREE_TYPE (type);
1872 tree ptrtype;
1873
1874 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1875
1876 STRIP_TYPE_NOPS (exp);
1877
1878 if (TREE_NO_WARNING (orig_exp))
1879 TREE_NO_WARNING (exp) = 1;
1880
1881 ptrtype = build_pointer_type (restype);
1882
1883 if (INDIRECT_REF_P (exp))
1884 return convert (ptrtype, TREE_OPERAND (exp, 0));
1885
1886 /* In C++ array compound literals are temporary objects unless they are
1887 const or appear in namespace scope, so they are destroyed too soon
1888 to use them for much of anything (c++/53220). */
1889 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1890 {
1891 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1892 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1893 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1894 "converting an array compound literal to a pointer "
1895 "is ill-formed in C++");
1896 }
1897
1898 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1899 return convert (ptrtype, adr);
1900 }
1901
1902 /* Convert the function expression EXP to a pointer. */
1903 static tree
function_to_pointer_conversion(location_t loc,tree exp)1904 function_to_pointer_conversion (location_t loc, tree exp)
1905 {
1906 tree orig_exp = exp;
1907
1908 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1909
1910 STRIP_TYPE_NOPS (exp);
1911
1912 if (TREE_NO_WARNING (orig_exp))
1913 TREE_NO_WARNING (exp) = 1;
1914
1915 return build_unary_op (loc, ADDR_EXPR, exp, false);
1916 }
1917
1918 /* Mark EXP as read, not just set, for set but not used -Wunused
1919 warning purposes. */
1920
1921 void
mark_exp_read(tree exp)1922 mark_exp_read (tree exp)
1923 {
1924 switch (TREE_CODE (exp))
1925 {
1926 case VAR_DECL:
1927 case PARM_DECL:
1928 DECL_READ_P (exp) = 1;
1929 break;
1930 case ARRAY_REF:
1931 case COMPONENT_REF:
1932 case MODIFY_EXPR:
1933 case REALPART_EXPR:
1934 case IMAGPART_EXPR:
1935 CASE_CONVERT:
1936 case ADDR_EXPR:
1937 case VIEW_CONVERT_EXPR:
1938 mark_exp_read (TREE_OPERAND (exp, 0));
1939 break;
1940 case COMPOUND_EXPR:
1941 case C_MAYBE_CONST_EXPR:
1942 mark_exp_read (TREE_OPERAND (exp, 1));
1943 break;
1944 default:
1945 break;
1946 }
1947 }
1948
1949 /* Perform the default conversion of arrays and functions to pointers.
1950 Return the result of converting EXP. For any other expression, just
1951 return EXP.
1952
1953 LOC is the location of the expression. */
1954
1955 struct c_expr
default_function_array_conversion(location_t loc,struct c_expr exp)1956 default_function_array_conversion (location_t loc, struct c_expr exp)
1957 {
1958 tree orig_exp = exp.value;
1959 tree type = TREE_TYPE (exp.value);
1960 enum tree_code code = TREE_CODE (type);
1961
1962 switch (code)
1963 {
1964 case ARRAY_TYPE:
1965 {
1966 bool not_lvalue = false;
1967 bool lvalue_array_p;
1968
1969 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1970 || CONVERT_EXPR_P (exp.value))
1971 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1972 {
1973 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1974 not_lvalue = true;
1975 exp.value = TREE_OPERAND (exp.value, 0);
1976 }
1977
1978 if (TREE_NO_WARNING (orig_exp))
1979 TREE_NO_WARNING (exp.value) = 1;
1980
1981 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1982 if (!flag_isoc99 && !lvalue_array_p)
1983 {
1984 /* Before C99, non-lvalue arrays do not decay to pointers.
1985 Normally, using such an array would be invalid; but it can
1986 be used correctly inside sizeof or as a statement expression.
1987 Thus, do not give an error here; an error will result later. */
1988 return exp;
1989 }
1990
1991 exp.value = array_to_pointer_conversion (loc, exp.value);
1992 }
1993 break;
1994 case FUNCTION_TYPE:
1995 exp.value = function_to_pointer_conversion (loc, exp.value);
1996 break;
1997 default:
1998 break;
1999 }
2000
2001 return exp;
2002 }
2003
2004 struct c_expr
default_function_array_read_conversion(location_t loc,struct c_expr exp)2005 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2006 {
2007 mark_exp_read (exp.value);
2008 return default_function_array_conversion (loc, exp);
2009 }
2010
2011 /* Return whether EXPR should be treated as an atomic lvalue for the
2012 purposes of load and store handling. */
2013
2014 static bool
really_atomic_lvalue(tree expr)2015 really_atomic_lvalue (tree expr)
2016 {
2017 if (error_operand_p (expr))
2018 return false;
2019 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2020 return false;
2021 if (!lvalue_p (expr))
2022 return false;
2023
2024 /* Ignore _Atomic on register variables, since their addresses can't
2025 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2026 sequences wouldn't work. Ignore _Atomic on structures containing
2027 bit-fields, since accessing elements of atomic structures or
2028 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2029 it's undefined at translation time or execution time, and the
2030 normal atomic sequences again wouldn't work. */
2031 while (handled_component_p (expr))
2032 {
2033 if (TREE_CODE (expr) == COMPONENT_REF
2034 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2035 return false;
2036 expr = TREE_OPERAND (expr, 0);
2037 }
2038 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2039 return false;
2040 return true;
2041 }
2042
2043 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2044 including converting functions and arrays to pointers if CONVERT_P.
2045 If READ_P, also mark the expression as having been read. */
2046
2047 struct c_expr
convert_lvalue_to_rvalue(location_t loc,struct c_expr exp,bool convert_p,bool read_p)2048 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2049 bool convert_p, bool read_p)
2050 {
2051 if (read_p)
2052 mark_exp_read (exp.value);
2053 if (convert_p)
2054 exp = default_function_array_conversion (loc, exp);
2055 if (really_atomic_lvalue (exp.value))
2056 {
2057 vec<tree, va_gc> *params;
2058 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2059 tree expr_type = TREE_TYPE (exp.value);
2060 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2061 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2062
2063 gcc_assert (TYPE_ATOMIC (expr_type));
2064
2065 /* Expansion of a generic atomic load may require an addition
2066 element, so allocate enough to prevent a resize. */
2067 vec_alloc (params, 4);
2068
2069 /* Remove the qualifiers for the rest of the expressions and
2070 create the VAL temp variable to hold the RHS. */
2071 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2072 tmp = create_tmp_var_raw (nonatomic_type);
2073 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2074 TREE_ADDRESSABLE (tmp) = 1;
2075 TREE_NO_WARNING (tmp) = 1;
2076
2077 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2078 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2079 params->quick_push (expr_addr);
2080 params->quick_push (tmp_addr);
2081 params->quick_push (seq_cst);
2082 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2083
2084 /* EXPR is always read. */
2085 mark_exp_read (exp.value);
2086
2087 /* Return tmp which contains the value loaded. */
2088 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2089 NULL_TREE, NULL_TREE);
2090 }
2091 return exp;
2092 }
2093
2094 /* EXP is an expression of integer type. Apply the integer promotions
2095 to it and return the promoted value. */
2096
2097 tree
perform_integral_promotions(tree exp)2098 perform_integral_promotions (tree exp)
2099 {
2100 tree type = TREE_TYPE (exp);
2101 enum tree_code code = TREE_CODE (type);
2102
2103 gcc_assert (INTEGRAL_TYPE_P (type));
2104
2105 /* Normally convert enums to int,
2106 but convert wide enums to something wider. */
2107 if (code == ENUMERAL_TYPE)
2108 {
2109 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2110 TYPE_PRECISION (integer_type_node)),
2111 ((TYPE_PRECISION (type)
2112 >= TYPE_PRECISION (integer_type_node))
2113 && TYPE_UNSIGNED (type)));
2114
2115 return convert (type, exp);
2116 }
2117
2118 /* ??? This should no longer be needed now bit-fields have their
2119 proper types. */
2120 if (TREE_CODE (exp) == COMPONENT_REF
2121 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2122 /* If it's thinner than an int, promote it like a
2123 c_promoting_integer_type_p, otherwise leave it alone. */
2124 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2125 TYPE_PRECISION (integer_type_node)) < 0)
2126 return convert (integer_type_node, exp);
2127
2128 if (c_promoting_integer_type_p (type))
2129 {
2130 /* Preserve unsignedness if not really getting any wider. */
2131 if (TYPE_UNSIGNED (type)
2132 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2133 return convert (unsigned_type_node, exp);
2134
2135 return convert (integer_type_node, exp);
2136 }
2137
2138 return exp;
2139 }
2140
2141
2142 /* Perform default promotions for C data used in expressions.
2143 Enumeral types or short or char are converted to int.
2144 In addition, manifest constants symbols are replaced by their values. */
2145
2146 tree
default_conversion(tree exp)2147 default_conversion (tree exp)
2148 {
2149 tree orig_exp;
2150 tree type = TREE_TYPE (exp);
2151 enum tree_code code = TREE_CODE (type);
2152 tree promoted_type;
2153
2154 mark_exp_read (exp);
2155
2156 /* Functions and arrays have been converted during parsing. */
2157 gcc_assert (code != FUNCTION_TYPE);
2158 if (code == ARRAY_TYPE)
2159 return exp;
2160
2161 /* Constants can be used directly unless they're not loadable. */
2162 if (TREE_CODE (exp) == CONST_DECL)
2163 exp = DECL_INITIAL (exp);
2164
2165 /* Strip no-op conversions. */
2166 orig_exp = exp;
2167 STRIP_TYPE_NOPS (exp);
2168
2169 if (TREE_NO_WARNING (orig_exp))
2170 TREE_NO_WARNING (exp) = 1;
2171
2172 if (code == VOID_TYPE)
2173 {
2174 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2175 "void value not ignored as it ought to be");
2176 return error_mark_node;
2177 }
2178
2179 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2180 if (exp == error_mark_node)
2181 return error_mark_node;
2182
2183 promoted_type = targetm.promoted_type (type);
2184 if (promoted_type)
2185 return convert (promoted_type, exp);
2186
2187 if (INTEGRAL_TYPE_P (type))
2188 return perform_integral_promotions (exp);
2189
2190 return exp;
2191 }
2192
2193 /* Look up COMPONENT in a structure or union TYPE.
2194
2195 If the component name is not found, returns NULL_TREE. Otherwise,
2196 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2197 stepping down the chain to the component, which is in the last
2198 TREE_VALUE of the list. Normally the list is of length one, but if
2199 the component is embedded within (nested) anonymous structures or
2200 unions, the list steps down the chain to the component. */
2201
2202 static tree
lookup_field(tree type,tree component)2203 lookup_field (tree type, tree component)
2204 {
2205 tree field;
2206
2207 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2208 to the field elements. Use a binary search on this array to quickly
2209 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2210 will always be set for structures which have many elements. */
2211
2212 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2213 {
2214 int bot, top, half;
2215 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2216
2217 field = TYPE_FIELDS (type);
2218 bot = 0;
2219 top = TYPE_LANG_SPECIFIC (type)->s->len;
2220 while (top - bot > 1)
2221 {
2222 half = (top - bot + 1) >> 1;
2223 field = field_array[bot+half];
2224
2225 if (DECL_NAME (field) == NULL_TREE)
2226 {
2227 /* Step through all anon unions in linear fashion. */
2228 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2229 {
2230 field = field_array[bot++];
2231 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2232 {
2233 tree anon = lookup_field (TREE_TYPE (field), component);
2234
2235 if (anon)
2236 return tree_cons (NULL_TREE, field, anon);
2237
2238 /* The Plan 9 compiler permits referring
2239 directly to an anonymous struct/union field
2240 using a typedef name. */
2241 if (flag_plan9_extensions
2242 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2243 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2244 == TYPE_DECL)
2245 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2246 == component))
2247 break;
2248 }
2249 }
2250
2251 /* Entire record is only anon unions. */
2252 if (bot > top)
2253 return NULL_TREE;
2254
2255 /* Restart the binary search, with new lower bound. */
2256 continue;
2257 }
2258
2259 if (DECL_NAME (field) == component)
2260 break;
2261 if (DECL_NAME (field) < component)
2262 bot += half;
2263 else
2264 top = bot + half;
2265 }
2266
2267 if (DECL_NAME (field_array[bot]) == component)
2268 field = field_array[bot];
2269 else if (DECL_NAME (field) != component)
2270 return NULL_TREE;
2271 }
2272 else
2273 {
2274 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2275 {
2276 if (DECL_NAME (field) == NULL_TREE
2277 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2278 {
2279 tree anon = lookup_field (TREE_TYPE (field), component);
2280
2281 if (anon)
2282 return tree_cons (NULL_TREE, field, anon);
2283
2284 /* The Plan 9 compiler permits referring directly to an
2285 anonymous struct/union field using a typedef
2286 name. */
2287 if (flag_plan9_extensions
2288 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2289 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2290 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2291 == component))
2292 break;
2293 }
2294
2295 if (DECL_NAME (field) == component)
2296 break;
2297 }
2298
2299 if (field == NULL_TREE)
2300 return NULL_TREE;
2301 }
2302
2303 return tree_cons (NULL_TREE, field, NULL_TREE);
2304 }
2305
2306 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2307
2308 static void
lookup_field_fuzzy_find_candidates(tree type,tree component,vec<tree> * candidates)2309 lookup_field_fuzzy_find_candidates (tree type, tree component,
2310 vec<tree> *candidates)
2311 {
2312 tree field;
2313 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2314 {
2315 if (DECL_NAME (field) == NULL_TREE
2316 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2317 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2318 candidates);
2319
2320 if (DECL_NAME (field))
2321 candidates->safe_push (DECL_NAME (field));
2322 }
2323 }
2324
2325 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2326 rather than returning a TREE_LIST for an exact match. */
2327
2328 static tree
lookup_field_fuzzy(tree type,tree component)2329 lookup_field_fuzzy (tree type, tree component)
2330 {
2331 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2332
2333 /* First, gather a list of candidates. */
2334 auto_vec <tree> candidates;
2335
2336 lookup_field_fuzzy_find_candidates (type, component,
2337 &candidates);
2338
2339 return find_closest_identifier (component, &candidates);
2340 }
2341
2342 /* Support function for build_component_ref's error-handling.
2343
2344 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2345 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2346
2347 static bool
should_suggest_deref_p(tree datum_type)2348 should_suggest_deref_p (tree datum_type)
2349 {
2350 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2351 allows "." for ptrs; we could be handling a failed attempt
2352 to access a property. */
2353 if (c_dialect_objc ())
2354 return false;
2355
2356 /* Only suggest it for pointers... */
2357 if (TREE_CODE (datum_type) != POINTER_TYPE)
2358 return false;
2359
2360 /* ...to structs/unions. */
2361 tree underlying_type = TREE_TYPE (datum_type);
2362 enum tree_code code = TREE_CODE (underlying_type);
2363 if (code == RECORD_TYPE || code == UNION_TYPE)
2364 return true;
2365 else
2366 return false;
2367 }
2368
2369 /* Make an expression to refer to the COMPONENT field of structure or
2370 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2371 location of the COMPONENT_REF. COMPONENT_LOC is the location
2372 of COMPONENT. */
2373
2374 tree
build_component_ref(location_t loc,tree datum,tree component,location_t component_loc)2375 build_component_ref (location_t loc, tree datum, tree component,
2376 location_t component_loc)
2377 {
2378 tree type = TREE_TYPE (datum);
2379 enum tree_code code = TREE_CODE (type);
2380 tree field = NULL;
2381 tree ref;
2382 bool datum_lvalue = lvalue_p (datum);
2383
2384 if (!objc_is_public (datum, component))
2385 return error_mark_node;
2386
2387 /* Detect Objective-C property syntax object.property. */
2388 if (c_dialect_objc ()
2389 && (ref = objc_maybe_build_component_ref (datum, component)))
2390 return ref;
2391
2392 /* See if there is a field or component with name COMPONENT. */
2393
2394 if (code == RECORD_TYPE || code == UNION_TYPE)
2395 {
2396 if (!COMPLETE_TYPE_P (type))
2397 {
2398 c_incomplete_type_error (loc, NULL_TREE, type);
2399 return error_mark_node;
2400 }
2401
2402 field = lookup_field (type, component);
2403
2404 if (!field)
2405 {
2406 tree guessed_id = lookup_field_fuzzy (type, component);
2407 if (guessed_id)
2408 {
2409 /* Attempt to provide a fixit replacement hint, if
2410 we have a valid range for the component. */
2411 location_t reported_loc
2412 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2413 gcc_rich_location rich_loc (reported_loc);
2414 if (component_loc != UNKNOWN_LOCATION)
2415 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2416 error_at (&rich_loc,
2417 "%qT has no member named %qE; did you mean %qE?",
2418 type, component, guessed_id);
2419 }
2420 else
2421 error_at (loc, "%qT has no member named %qE", type, component);
2422 return error_mark_node;
2423 }
2424
2425 /* Accessing elements of atomic structures or unions is undefined
2426 behavior (C11 6.5.2.3#5). */
2427 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2428 {
2429 if (code == RECORD_TYPE)
2430 warning_at (loc, 0, "accessing a member %qE of an atomic "
2431 "structure %qE", component, datum);
2432 else
2433 warning_at (loc, 0, "accessing a member %qE of an atomic "
2434 "union %qE", component, datum);
2435 }
2436
2437 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2438 This might be better solved in future the way the C++ front
2439 end does it - by giving the anonymous entities each a
2440 separate name and type, and then have build_component_ref
2441 recursively call itself. We can't do that here. */
2442 do
2443 {
2444 tree subdatum = TREE_VALUE (field);
2445 int quals;
2446 tree subtype;
2447 bool use_datum_quals;
2448
2449 if (TREE_TYPE (subdatum) == error_mark_node)
2450 return error_mark_node;
2451
2452 /* If this is an rvalue, it does not have qualifiers in C
2453 standard terms and we must avoid propagating such
2454 qualifiers down to a non-lvalue array that is then
2455 converted to a pointer. */
2456 use_datum_quals = (datum_lvalue
2457 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2458
2459 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2460 if (use_datum_quals)
2461 quals |= TYPE_QUALS (TREE_TYPE (datum));
2462 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2463
2464 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2465 NULL_TREE);
2466 SET_EXPR_LOCATION (ref, loc);
2467 if (TREE_READONLY (subdatum)
2468 || (use_datum_quals && TREE_READONLY (datum)))
2469 TREE_READONLY (ref) = 1;
2470 if (TREE_THIS_VOLATILE (subdatum)
2471 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2472 TREE_THIS_VOLATILE (ref) = 1;
2473
2474 if (TREE_DEPRECATED (subdatum))
2475 warn_deprecated_use (subdatum, NULL_TREE);
2476
2477 datum = ref;
2478
2479 field = TREE_CHAIN (field);
2480 }
2481 while (field);
2482
2483 return ref;
2484 }
2485 else if (should_suggest_deref_p (type))
2486 {
2487 /* Special-case the error message for "ptr.field" for the case
2488 where the user has confused "." vs "->". */
2489 rich_location richloc (line_table, loc);
2490 /* "loc" should be the "." token. */
2491 richloc.add_fixit_replace ("->");
2492 error_at (&richloc,
2493 "%qE is a pointer; did you mean to use %<->%>?",
2494 datum);
2495 return error_mark_node;
2496 }
2497 else if (code != ERROR_MARK)
2498 error_at (loc,
2499 "request for member %qE in something not a structure or union",
2500 component);
2501
2502 return error_mark_node;
2503 }
2504
2505 /* Given an expression PTR for a pointer, return an expression
2506 for the value pointed to.
2507 ERRORSTRING is the name of the operator to appear in error messages.
2508
2509 LOC is the location to use for the generated tree. */
2510
2511 tree
build_indirect_ref(location_t loc,tree ptr,ref_operator errstring)2512 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2513 {
2514 tree pointer = default_conversion (ptr);
2515 tree type = TREE_TYPE (pointer);
2516 tree ref;
2517
2518 if (TREE_CODE (type) == POINTER_TYPE)
2519 {
2520 if (CONVERT_EXPR_P (pointer)
2521 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2522 {
2523 /* If a warning is issued, mark it to avoid duplicates from
2524 the backend. This only needs to be done at
2525 warn_strict_aliasing > 2. */
2526 if (warn_strict_aliasing > 2)
2527 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2528 type, TREE_OPERAND (pointer, 0)))
2529 TREE_NO_WARNING (pointer) = 1;
2530 }
2531
2532 if (TREE_CODE (pointer) == ADDR_EXPR
2533 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2534 == TREE_TYPE (type)))
2535 {
2536 ref = TREE_OPERAND (pointer, 0);
2537 protected_set_expr_location (ref, loc);
2538 return ref;
2539 }
2540 else
2541 {
2542 tree t = TREE_TYPE (type);
2543
2544 ref = build1 (INDIRECT_REF, t, pointer);
2545
2546 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2547 {
2548 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2549 {
2550 error_at (loc, "dereferencing pointer to incomplete type "
2551 "%qT", t);
2552 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2553 }
2554 return error_mark_node;
2555 }
2556 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2557 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2558
2559 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2560 so that we get the proper error message if the result is used
2561 to assign to. Also, &* is supposed to be a no-op.
2562 And ANSI C seems to specify that the type of the result
2563 should be the const type. */
2564 /* A de-reference of a pointer to const is not a const. It is valid
2565 to change it via some other pointer. */
2566 TREE_READONLY (ref) = TYPE_READONLY (t);
2567 TREE_SIDE_EFFECTS (ref)
2568 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2569 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2570 protected_set_expr_location (ref, loc);
2571 return ref;
2572 }
2573 }
2574 else if (TREE_CODE (pointer) != ERROR_MARK)
2575 invalid_indirection_error (loc, type, errstring);
2576
2577 return error_mark_node;
2578 }
2579
2580 /* This handles expressions of the form "a[i]", which denotes
2581 an array reference.
2582
2583 This is logically equivalent in C to *(a+i), but we may do it differently.
2584 If A is a variable or a member, we generate a primitive ARRAY_REF.
2585 This avoids forcing the array out of registers, and can work on
2586 arrays that are not lvalues (for example, members of structures returned
2587 by functions).
2588
2589 For vector types, allow vector[i] but not i[vector], and create
2590 *(((type*)&vectortype) + i) for the expression.
2591
2592 LOC is the location to use for the returned expression. */
2593
2594 tree
build_array_ref(location_t loc,tree array,tree index)2595 build_array_ref (location_t loc, tree array, tree index)
2596 {
2597 tree ret;
2598 bool swapped = false;
2599 if (TREE_TYPE (array) == error_mark_node
2600 || TREE_TYPE (index) == error_mark_node)
2601 return error_mark_node;
2602
2603 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2604 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2605 /* Allow vector[index] but not index[vector]. */
2606 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2607 {
2608 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2609 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2610 {
2611 error_at (loc,
2612 "subscripted value is neither array nor pointer nor vector");
2613
2614 return error_mark_node;
2615 }
2616 std::swap (array, index);
2617 swapped = true;
2618 }
2619
2620 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2621 {
2622 error_at (loc, "array subscript is not an integer");
2623 return error_mark_node;
2624 }
2625
2626 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2627 {
2628 error_at (loc, "subscripted value is pointer to function");
2629 return error_mark_node;
2630 }
2631
2632 /* ??? Existing practice has been to warn only when the char
2633 index is syntactically the index, not for char[array]. */
2634 if (!swapped)
2635 warn_array_subscript_with_type_char (loc, index);
2636
2637 /* Apply default promotions *after* noticing character types. */
2638 index = default_conversion (index);
2639 if (index == error_mark_node)
2640 return error_mark_node;
2641
2642 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2643
2644 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2645 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2646
2647 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2648 {
2649 tree rval, type;
2650
2651 /* An array that is indexed by a non-constant
2652 cannot be stored in a register; we must be able to do
2653 address arithmetic on its address.
2654 Likewise an array of elements of variable size. */
2655 if (TREE_CODE (index) != INTEGER_CST
2656 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2657 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2658 {
2659 if (!c_mark_addressable (array, true))
2660 return error_mark_node;
2661 }
2662 /* An array that is indexed by a constant value which is not within
2663 the array bounds cannot be stored in a register either; because we
2664 would get a crash in store_bit_field/extract_bit_field when trying
2665 to access a non-existent part of the register. */
2666 if (TREE_CODE (index) == INTEGER_CST
2667 && TYPE_DOMAIN (TREE_TYPE (array))
2668 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2669 {
2670 if (!c_mark_addressable (array))
2671 return error_mark_node;
2672 }
2673
2674 if ((pedantic || warn_c90_c99_compat)
2675 && ! was_vector)
2676 {
2677 tree foo = array;
2678 while (TREE_CODE (foo) == COMPONENT_REF)
2679 foo = TREE_OPERAND (foo, 0);
2680 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2681 pedwarn (loc, OPT_Wpedantic,
2682 "ISO C forbids subscripting %<register%> array");
2683 else if (!lvalue_p (foo))
2684 pedwarn_c90 (loc, OPT_Wpedantic,
2685 "ISO C90 forbids subscripting non-lvalue "
2686 "array");
2687 }
2688
2689 type = TREE_TYPE (TREE_TYPE (array));
2690 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2691 /* Array ref is const/volatile if the array elements are
2692 or if the array is. */
2693 TREE_READONLY (rval)
2694 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2695 | TREE_READONLY (array));
2696 TREE_SIDE_EFFECTS (rval)
2697 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2698 | TREE_SIDE_EFFECTS (array));
2699 TREE_THIS_VOLATILE (rval)
2700 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2701 /* This was added by rms on 16 Nov 91.
2702 It fixes vol struct foo *a; a->elts[1]
2703 in an inline function.
2704 Hope it doesn't break something else. */
2705 | TREE_THIS_VOLATILE (array));
2706 ret = require_complete_type (loc, rval);
2707 protected_set_expr_location (ret, loc);
2708 if (non_lvalue)
2709 ret = non_lvalue_loc (loc, ret);
2710 return ret;
2711 }
2712 else
2713 {
2714 tree ar = default_conversion (array);
2715
2716 if (ar == error_mark_node)
2717 return ar;
2718
2719 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2720 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2721
2722 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2723 index, false),
2724 RO_ARRAY_INDEXING);
2725 if (non_lvalue)
2726 ret = non_lvalue_loc (loc, ret);
2727 return ret;
2728 }
2729 }
2730
2731 /* Build an external reference to identifier ID. FUN indicates
2732 whether this will be used for a function call. LOC is the source
2733 location of the identifier. This sets *TYPE to the type of the
2734 identifier, which is not the same as the type of the returned value
2735 for CONST_DECLs defined as enum constants. If the type of the
2736 identifier is not available, *TYPE is set to NULL. */
2737 tree
build_external_ref(location_t loc,tree id,bool fun,tree * type)2738 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2739 {
2740 tree ref;
2741 tree decl = lookup_name (id);
2742
2743 /* In Objective-C, an instance variable (ivar) may be preferred to
2744 whatever lookup_name() found. */
2745 decl = objc_lookup_ivar (decl, id);
2746
2747 *type = NULL;
2748 if (decl && decl != error_mark_node)
2749 {
2750 ref = decl;
2751 *type = TREE_TYPE (ref);
2752 }
2753 else if (fun)
2754 /* Implicit function declaration. */
2755 ref = implicitly_declare (loc, id);
2756 else if (decl == error_mark_node)
2757 /* Don't complain about something that's already been
2758 complained about. */
2759 return error_mark_node;
2760 else
2761 {
2762 undeclared_variable (loc, id);
2763 return error_mark_node;
2764 }
2765
2766 if (TREE_TYPE (ref) == error_mark_node)
2767 return error_mark_node;
2768
2769 if (TREE_DEPRECATED (ref))
2770 warn_deprecated_use (ref, NULL_TREE);
2771
2772 /* Recursive call does not count as usage. */
2773 if (ref != current_function_decl)
2774 {
2775 TREE_USED (ref) = 1;
2776 }
2777
2778 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2779 {
2780 if (!in_sizeof && !in_typeof)
2781 C_DECL_USED (ref) = 1;
2782 else if (DECL_INITIAL (ref) == NULL_TREE
2783 && DECL_EXTERNAL (ref)
2784 && !TREE_PUBLIC (ref))
2785 record_maybe_used_decl (ref);
2786 }
2787
2788 if (TREE_CODE (ref) == CONST_DECL)
2789 {
2790 used_types_insert (TREE_TYPE (ref));
2791
2792 if (warn_cxx_compat
2793 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2794 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2795 {
2796 warning_at (loc, OPT_Wc___compat,
2797 ("enum constant defined in struct or union "
2798 "is not visible in C++"));
2799 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2800 }
2801
2802 ref = DECL_INITIAL (ref);
2803 TREE_CONSTANT (ref) = 1;
2804 }
2805 else if (current_function_decl != NULL_TREE
2806 && !DECL_FILE_SCOPE_P (current_function_decl)
2807 && (VAR_OR_FUNCTION_DECL_P (ref)
2808 || TREE_CODE (ref) == PARM_DECL))
2809 {
2810 tree context = decl_function_context (ref);
2811
2812 if (context != NULL_TREE && context != current_function_decl)
2813 DECL_NONLOCAL (ref) = 1;
2814 }
2815 /* C99 6.7.4p3: An inline definition of a function with external
2816 linkage ... shall not contain a reference to an identifier with
2817 internal linkage. */
2818 else if (current_function_decl != NULL_TREE
2819 && DECL_DECLARED_INLINE_P (current_function_decl)
2820 && DECL_EXTERNAL (current_function_decl)
2821 && VAR_OR_FUNCTION_DECL_P (ref)
2822 && (!VAR_P (ref) || TREE_STATIC (ref))
2823 && ! TREE_PUBLIC (ref)
2824 && DECL_CONTEXT (ref) != current_function_decl)
2825 record_inline_static (loc, current_function_decl, ref,
2826 csi_internal);
2827
2828 return ref;
2829 }
2830
2831 /* Record details of decls possibly used inside sizeof or typeof. */
2832 struct maybe_used_decl
2833 {
2834 /* The decl. */
2835 tree decl;
2836 /* The level seen at (in_sizeof + in_typeof). */
2837 int level;
2838 /* The next one at this level or above, or NULL. */
2839 struct maybe_used_decl *next;
2840 };
2841
2842 static struct maybe_used_decl *maybe_used_decls;
2843
2844 /* Record that DECL, an undefined static function reference seen
2845 inside sizeof or typeof, might be used if the operand of sizeof is
2846 a VLA type or the operand of typeof is a variably modified
2847 type. */
2848
2849 static void
record_maybe_used_decl(tree decl)2850 record_maybe_used_decl (tree decl)
2851 {
2852 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2853 t->decl = decl;
2854 t->level = in_sizeof + in_typeof;
2855 t->next = maybe_used_decls;
2856 maybe_used_decls = t;
2857 }
2858
2859 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2860 USED is false, just discard them. If it is true, mark them used
2861 (if no longer inside sizeof or typeof) or move them to the next
2862 level up (if still inside sizeof or typeof). */
2863
2864 void
pop_maybe_used(bool used)2865 pop_maybe_used (bool used)
2866 {
2867 struct maybe_used_decl *p = maybe_used_decls;
2868 int cur_level = in_sizeof + in_typeof;
2869 while (p && p->level > cur_level)
2870 {
2871 if (used)
2872 {
2873 if (cur_level == 0)
2874 C_DECL_USED (p->decl) = 1;
2875 else
2876 p->level = cur_level;
2877 }
2878 p = p->next;
2879 }
2880 if (!used || cur_level == 0)
2881 maybe_used_decls = p;
2882 }
2883
2884 /* Return the result of sizeof applied to EXPR. */
2885
2886 struct c_expr
c_expr_sizeof_expr(location_t loc,struct c_expr expr)2887 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2888 {
2889 struct c_expr ret;
2890 if (expr.value == error_mark_node)
2891 {
2892 ret.value = error_mark_node;
2893 ret.original_code = ERROR_MARK;
2894 ret.original_type = NULL;
2895 pop_maybe_used (false);
2896 }
2897 else
2898 {
2899 bool expr_const_operands = true;
2900
2901 if (TREE_CODE (expr.value) == PARM_DECL
2902 && C_ARRAY_PARAMETER (expr.value))
2903 {
2904 if (warning_at (loc, OPT_Wsizeof_array_argument,
2905 "%<sizeof%> on array function parameter %qE will "
2906 "return size of %qT", expr.value,
2907 TREE_TYPE (expr.value)))
2908 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2909 }
2910 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2911 &expr_const_operands);
2912 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2913 c_last_sizeof_arg = expr.value;
2914 c_last_sizeof_loc = loc;
2915 ret.original_code = SIZEOF_EXPR;
2916 ret.original_type = NULL;
2917 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2918 {
2919 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2920 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2921 folded_expr, ret.value);
2922 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2923 SET_EXPR_LOCATION (ret.value, loc);
2924 }
2925 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2926 }
2927 return ret;
2928 }
2929
2930 /* Return the result of sizeof applied to T, a structure for the type
2931 name passed to sizeof (rather than the type itself). LOC is the
2932 location of the original expression. */
2933
2934 struct c_expr
c_expr_sizeof_type(location_t loc,struct c_type_name * t)2935 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2936 {
2937 tree type;
2938 struct c_expr ret;
2939 tree type_expr = NULL_TREE;
2940 bool type_expr_const = true;
2941 type = groktypename (t, &type_expr, &type_expr_const);
2942 ret.value = c_sizeof (loc, type);
2943 c_last_sizeof_arg = type;
2944 c_last_sizeof_loc = loc;
2945 ret.original_code = SIZEOF_EXPR;
2946 ret.original_type = NULL;
2947 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2948 && c_vla_type_p (type))
2949 {
2950 /* If the type is a [*] array, it is a VLA but is represented as
2951 having a size of zero. In such a case we must ensure that
2952 the result of sizeof does not get folded to a constant by
2953 c_fully_fold, because if the size is evaluated the result is
2954 not constant and so constraints on zero or negative size
2955 arrays must not be applied when this sizeof call is inside
2956 another array declarator. */
2957 if (!type_expr)
2958 type_expr = integer_zero_node;
2959 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2960 type_expr, ret.value);
2961 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2962 }
2963 pop_maybe_used (type != error_mark_node
2964 ? C_TYPE_VARIABLE_SIZE (type) : false);
2965 return ret;
2966 }
2967
2968 /* Build a function call to function FUNCTION with parameters PARAMS.
2969 The function call is at LOC.
2970 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2971 TREE_VALUE of each node is a parameter-expression.
2972 FUNCTION's data type may be a function type or a pointer-to-function. */
2973
2974 tree
build_function_call(location_t loc,tree function,tree params)2975 build_function_call (location_t loc, tree function, tree params)
2976 {
2977 vec<tree, va_gc> *v;
2978 tree ret;
2979
2980 vec_alloc (v, list_length (params));
2981 for (; params; params = TREE_CHAIN (params))
2982 v->quick_push (TREE_VALUE (params));
2983 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2984 vec_free (v);
2985 return ret;
2986 }
2987
2988 /* Give a note about the location of the declaration of DECL. */
2989
2990 static void
inform_declaration(tree decl)2991 inform_declaration (tree decl)
2992 {
2993 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2994 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2995 }
2996
2997 /* Build a function call to function FUNCTION with parameters PARAMS.
2998 ORIGTYPES, if not NULL, is a vector of types; each element is
2999 either NULL or the original type of the corresponding element in
3000 PARAMS. The original type may differ from TREE_TYPE of the
3001 parameter for enums. FUNCTION's data type may be a function type
3002 or pointer-to-function. This function changes the elements of
3003 PARAMS. */
3004
3005 tree
build_function_call_vec(location_t loc,vec<location_t> arg_loc,tree function,vec<tree,va_gc> * params,vec<tree,va_gc> * origtypes)3006 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3007 tree function, vec<tree, va_gc> *params,
3008 vec<tree, va_gc> *origtypes)
3009 {
3010 tree fntype, fundecl = NULL_TREE;
3011 tree name = NULL_TREE, result;
3012 tree tem;
3013 int nargs;
3014 tree *argarray;
3015
3016
3017 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3018 STRIP_TYPE_NOPS (function);
3019
3020 /* Convert anything with function type to a pointer-to-function. */
3021 if (TREE_CODE (function) == FUNCTION_DECL)
3022 {
3023 name = DECL_NAME (function);
3024
3025 if (flag_tm)
3026 tm_malloc_replacement (function);
3027 fundecl = function;
3028 /* Atomic functions have type checking/casting already done. They are
3029 often rewritten and don't match the original parameter list. */
3030 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3031 origtypes = NULL;
3032 }
3033 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3034 function = function_to_pointer_conversion (loc, function);
3035
3036 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3037 expressions, like those used for ObjC messenger dispatches. */
3038 if (params && !params->is_empty ())
3039 function = objc_rewrite_function_call (function, (*params)[0]);
3040
3041 function = c_fully_fold (function, false, NULL);
3042
3043 fntype = TREE_TYPE (function);
3044
3045 if (TREE_CODE (fntype) == ERROR_MARK)
3046 return error_mark_node;
3047
3048 if (!(TREE_CODE (fntype) == POINTER_TYPE
3049 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3050 {
3051 if (!flag_diagnostics_show_caret)
3052 error_at (loc,
3053 "called object %qE is not a function or function pointer",
3054 function);
3055 else if (DECL_P (function))
3056 {
3057 error_at (loc,
3058 "called object %qD is not a function or function pointer",
3059 function);
3060 inform_declaration (function);
3061 }
3062 else
3063 error_at (loc,
3064 "called object is not a function or function pointer");
3065 return error_mark_node;
3066 }
3067
3068 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3069 current_function_returns_abnormally = 1;
3070
3071 /* fntype now gets the type of function pointed to. */
3072 fntype = TREE_TYPE (fntype);
3073
3074 /* Convert the parameters to the types declared in the
3075 function prototype, or apply default promotions. */
3076
3077 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3078 origtypes, function, fundecl);
3079 if (nargs < 0)
3080 return error_mark_node;
3081
3082 /* Check that the function is called through a compatible prototype.
3083 If it is not, warn. */
3084 if (CONVERT_EXPR_P (function)
3085 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3086 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3087 && !comptypes (fntype, TREE_TYPE (tem)))
3088 {
3089 tree return_type = TREE_TYPE (fntype);
3090
3091 /* This situation leads to run-time undefined behavior. We can't,
3092 therefore, simply error unless we can prove that all possible
3093 executions of the program must execute the code. */
3094 warning_at (loc, 0, "function called through a non-compatible type");
3095
3096 if (VOID_TYPE_P (return_type)
3097 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3098 pedwarn (loc, 0,
3099 "function with qualified void return type called");
3100 }
3101
3102 argarray = vec_safe_address (params);
3103
3104 /* Check that arguments to builtin functions match the expectations. */
3105 if (fundecl
3106 && DECL_BUILT_IN (fundecl)
3107 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3108 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3109 argarray))
3110 return error_mark_node;
3111
3112 /* Check that the arguments to the function are valid. */
3113 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3114 nargs, argarray, &arg_loc);
3115
3116 if (name != NULL_TREE
3117 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3118 {
3119 if (require_constant_value)
3120 result
3121 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3122 function, nargs, argarray);
3123 else
3124 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3125 function, nargs, argarray);
3126 if (TREE_CODE (result) == NOP_EXPR
3127 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3128 STRIP_TYPE_NOPS (result);
3129 }
3130 else
3131 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3132 function, nargs, argarray);
3133 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3134 later. */
3135 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3136 TREE_NO_WARNING (result) = 1;
3137
3138 /* In this improbable scenario, a nested function returns a VM type.
3139 Create a TARGET_EXPR so that the call always has a LHS, much as
3140 what the C++ FE does for functions returning non-PODs. */
3141 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3142 {
3143 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3144 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3145 NULL_TREE, NULL_TREE);
3146 }
3147
3148 if (VOID_TYPE_P (TREE_TYPE (result)))
3149 {
3150 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3151 pedwarn (loc, 0,
3152 "function with qualified void return type called");
3153 return result;
3154 }
3155 return require_complete_type (loc, result);
3156 }
3157
3158 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3159
3160 tree
c_build_function_call_vec(location_t loc,vec<location_t> arg_loc,tree function,vec<tree,va_gc> * params,vec<tree,va_gc> * origtypes)3161 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3162 tree function, vec<tree, va_gc> *params,
3163 vec<tree, va_gc> *origtypes)
3164 {
3165 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3166 STRIP_TYPE_NOPS (function);
3167
3168 /* Convert anything with function type to a pointer-to-function. */
3169 if (TREE_CODE (function) == FUNCTION_DECL)
3170 {
3171 /* Implement type-directed function overloading for builtins.
3172 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3173 handle all the type checking. The result is a complete expression
3174 that implements this function call. */
3175 tree tem = resolve_overloaded_builtin (loc, function, params);
3176 if (tem)
3177 return tem;
3178 }
3179 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3180 }
3181
3182 /* Convert the argument expressions in the vector VALUES
3183 to the types in the list TYPELIST.
3184
3185 If TYPELIST is exhausted, or when an element has NULL as its type,
3186 perform the default conversions.
3187
3188 ORIGTYPES is the original types of the expressions in VALUES. This
3189 holds the type of enum values which have been converted to integral
3190 types. It may be NULL.
3191
3192 FUNCTION is a tree for the called function. It is used only for
3193 error messages, where it is formatted with %qE.
3194
3195 This is also where warnings about wrong number of args are generated.
3196
3197 ARG_LOC are locations of function arguments (if any).
3198
3199 Returns the actual number of arguments processed (which may be less
3200 than the length of VALUES in some error situations), or -1 on
3201 failure. */
3202
3203 static int
convert_arguments(location_t loc,vec<location_t> arg_loc,tree typelist,vec<tree,va_gc> * values,vec<tree,va_gc> * origtypes,tree function,tree fundecl)3204 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3205 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3206 tree function, tree fundecl)
3207 {
3208 tree typetail, val;
3209 unsigned int parmnum;
3210 bool error_args = false;
3211 const bool type_generic = fundecl
3212 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3213 bool type_generic_remove_excess_precision = false;
3214 bool type_generic_overflow_p = false;
3215 tree selector;
3216
3217 /* Change pointer to function to the function itself for
3218 diagnostics. */
3219 if (TREE_CODE (function) == ADDR_EXPR
3220 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3221 function = TREE_OPERAND (function, 0);
3222
3223 /* Handle an ObjC selector specially for diagnostics. */
3224 selector = objc_message_selector ();
3225
3226 /* For type-generic built-in functions, determine whether excess
3227 precision should be removed (classification) or not
3228 (comparison). */
3229 if (type_generic
3230 && DECL_BUILT_IN (fundecl)
3231 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3232 {
3233 switch (DECL_FUNCTION_CODE (fundecl))
3234 {
3235 case BUILT_IN_ISFINITE:
3236 case BUILT_IN_ISINF:
3237 case BUILT_IN_ISINF_SIGN:
3238 case BUILT_IN_ISNAN:
3239 case BUILT_IN_ISNORMAL:
3240 case BUILT_IN_FPCLASSIFY:
3241 type_generic_remove_excess_precision = true;
3242 break;
3243
3244 case BUILT_IN_ADD_OVERFLOW_P:
3245 case BUILT_IN_SUB_OVERFLOW_P:
3246 case BUILT_IN_MUL_OVERFLOW_P:
3247 /* The last argument of these type-generic builtins
3248 should not be promoted. */
3249 type_generic_overflow_p = true;
3250 break;
3251
3252 default:
3253 break;
3254 }
3255 }
3256
3257 /* Scan the given expressions and types, producing individual
3258 converted arguments. */
3259
3260 for (typetail = typelist, parmnum = 0;
3261 values && values->iterate (parmnum, &val);
3262 ++parmnum)
3263 {
3264 tree type = typetail ? TREE_VALUE (typetail) : 0;
3265 tree valtype = TREE_TYPE (val);
3266 tree rname = function;
3267 int argnum = parmnum + 1;
3268 const char *invalid_func_diag;
3269 bool excess_precision = false;
3270 bool npc;
3271 tree parmval;
3272 /* Some __atomic_* builtins have additional hidden argument at
3273 position 0. */
3274 location_t ploc
3275 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3276 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3277 : input_location;
3278
3279 if (type == void_type_node)
3280 {
3281 if (selector)
3282 error_at (loc, "too many arguments to method %qE", selector);
3283 else
3284 error_at (loc, "too many arguments to function %qE", function);
3285 inform_declaration (fundecl);
3286 return error_args ? -1 : (int) parmnum;
3287 }
3288
3289 if (selector && argnum > 2)
3290 {
3291 rname = selector;
3292 argnum -= 2;
3293 }
3294
3295 npc = null_pointer_constant_p (val);
3296
3297 /* If there is excess precision and a prototype, convert once to
3298 the required type rather than converting via the semantic
3299 type. Likewise without a prototype a float value represented
3300 as long double should be converted once to double. But for
3301 type-generic classification functions excess precision must
3302 be removed here. */
3303 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3304 && (type || !type_generic || !type_generic_remove_excess_precision))
3305 {
3306 val = TREE_OPERAND (val, 0);
3307 excess_precision = true;
3308 }
3309 val = c_fully_fold (val, false, NULL);
3310 STRIP_TYPE_NOPS (val);
3311
3312 val = require_complete_type (ploc, val);
3313
3314 /* Some floating-point arguments must be promoted to double when
3315 no type is specified by a prototype. This applies to
3316 arguments of type float, and to architecture-specific types
3317 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3318 bool promote_float_arg = false;
3319 if (type == NULL_TREE
3320 && TREE_CODE (valtype) == REAL_TYPE
3321 && (TYPE_PRECISION (valtype)
3322 <= TYPE_PRECISION (double_type_node))
3323 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3324 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3325 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3326 {
3327 /* Promote this argument, unless it has a _FloatN or
3328 _FloatNx type. */
3329 promote_float_arg = true;
3330 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3331 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3332 {
3333 promote_float_arg = false;
3334 break;
3335 }
3336 }
3337
3338 if (type != NULL_TREE)
3339 {
3340 /* Formal parm type is specified by a function prototype. */
3341
3342 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3343 {
3344 error_at (ploc, "type of formal parameter %d is incomplete",
3345 parmnum + 1);
3346 parmval = val;
3347 }
3348 else
3349 {
3350 tree origtype;
3351
3352 /* Optionally warn about conversions that
3353 differ from the default conversions. */
3354 if (warn_traditional_conversion || warn_traditional)
3355 {
3356 unsigned int formal_prec = TYPE_PRECISION (type);
3357
3358 if (INTEGRAL_TYPE_P (type)
3359 && TREE_CODE (valtype) == REAL_TYPE)
3360 warning_at (ploc, OPT_Wtraditional_conversion,
3361 "passing argument %d of %qE as integer rather "
3362 "than floating due to prototype",
3363 argnum, rname);
3364 if (INTEGRAL_TYPE_P (type)
3365 && TREE_CODE (valtype) == COMPLEX_TYPE)
3366 warning_at (ploc, OPT_Wtraditional_conversion,
3367 "passing argument %d of %qE as integer rather "
3368 "than complex due to prototype",
3369 argnum, rname);
3370 else if (TREE_CODE (type) == COMPLEX_TYPE
3371 && TREE_CODE (valtype) == REAL_TYPE)
3372 warning_at (ploc, OPT_Wtraditional_conversion,
3373 "passing argument %d of %qE as complex rather "
3374 "than floating due to prototype",
3375 argnum, rname);
3376 else if (TREE_CODE (type) == REAL_TYPE
3377 && INTEGRAL_TYPE_P (valtype))
3378 warning_at (ploc, OPT_Wtraditional_conversion,
3379 "passing argument %d of %qE as floating rather "
3380 "than integer due to prototype",
3381 argnum, rname);
3382 else if (TREE_CODE (type) == COMPLEX_TYPE
3383 && INTEGRAL_TYPE_P (valtype))
3384 warning_at (ploc, OPT_Wtraditional_conversion,
3385 "passing argument %d of %qE as complex rather "
3386 "than integer due to prototype",
3387 argnum, rname);
3388 else if (TREE_CODE (type) == REAL_TYPE
3389 && TREE_CODE (valtype) == COMPLEX_TYPE)
3390 warning_at (ploc, OPT_Wtraditional_conversion,
3391 "passing argument %d of %qE as floating rather "
3392 "than complex due to prototype",
3393 argnum, rname);
3394 /* ??? At some point, messages should be written about
3395 conversions between complex types, but that's too messy
3396 to do now. */
3397 else if (TREE_CODE (type) == REAL_TYPE
3398 && TREE_CODE (valtype) == REAL_TYPE)
3399 {
3400 /* Warn if any argument is passed as `float',
3401 since without a prototype it would be `double'. */
3402 if (formal_prec == TYPE_PRECISION (float_type_node)
3403 && type != dfloat32_type_node)
3404 warning_at (ploc, 0,
3405 "passing argument %d of %qE as %<float%> "
3406 "rather than %<double%> due to prototype",
3407 argnum, rname);
3408
3409 /* Warn if mismatch between argument and prototype
3410 for decimal float types. Warn of conversions with
3411 binary float types and of precision narrowing due to
3412 prototype. */
3413 else if (type != valtype
3414 && (type == dfloat32_type_node
3415 || type == dfloat64_type_node
3416 || type == dfloat128_type_node
3417 || valtype == dfloat32_type_node
3418 || valtype == dfloat64_type_node
3419 || valtype == dfloat128_type_node)
3420 && (formal_prec
3421 <= TYPE_PRECISION (valtype)
3422 || (type == dfloat128_type_node
3423 && (valtype
3424 != dfloat64_type_node
3425 && (valtype
3426 != dfloat32_type_node)))
3427 || (type == dfloat64_type_node
3428 && (valtype
3429 != dfloat32_type_node))))
3430 warning_at (ploc, 0,
3431 "passing argument %d of %qE as %qT "
3432 "rather than %qT due to prototype",
3433 argnum, rname, type, valtype);
3434
3435 }
3436 /* Detect integer changing in width or signedness.
3437 These warnings are only activated with
3438 -Wtraditional-conversion, not with -Wtraditional. */
3439 else if (warn_traditional_conversion
3440 && INTEGRAL_TYPE_P (type)
3441 && INTEGRAL_TYPE_P (valtype))
3442 {
3443 tree would_have_been = default_conversion (val);
3444 tree type1 = TREE_TYPE (would_have_been);
3445
3446 if (val == error_mark_node)
3447 /* VAL could have been of incomplete type. */;
3448 else if (TREE_CODE (type) == ENUMERAL_TYPE
3449 && (TYPE_MAIN_VARIANT (type)
3450 == TYPE_MAIN_VARIANT (valtype)))
3451 /* No warning if function asks for enum
3452 and the actual arg is that enum type. */
3453 ;
3454 else if (formal_prec != TYPE_PRECISION (type1))
3455 warning_at (ploc, OPT_Wtraditional_conversion,
3456 "passing argument %d of %qE "
3457 "with different width due to prototype",
3458 argnum, rname);
3459 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3460 ;
3461 /* Don't complain if the formal parameter type
3462 is an enum, because we can't tell now whether
3463 the value was an enum--even the same enum. */
3464 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3465 ;
3466 else if (TREE_CODE (val) == INTEGER_CST
3467 && int_fits_type_p (val, type))
3468 /* Change in signedness doesn't matter
3469 if a constant value is unaffected. */
3470 ;
3471 /* If the value is extended from a narrower
3472 unsigned type, it doesn't matter whether we
3473 pass it as signed or unsigned; the value
3474 certainly is the same either way. */
3475 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3476 && TYPE_UNSIGNED (valtype))
3477 ;
3478 else if (TYPE_UNSIGNED (type))
3479 warning_at (ploc, OPT_Wtraditional_conversion,
3480 "passing argument %d of %qE "
3481 "as unsigned due to prototype",
3482 argnum, rname);
3483 else
3484 warning_at (ploc, OPT_Wtraditional_conversion,
3485 "passing argument %d of %qE "
3486 "as signed due to prototype",
3487 argnum, rname);
3488 }
3489 }
3490
3491 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3492 sake of better warnings from convert_and_check. */
3493 if (excess_precision)
3494 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3495 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3496 parmval = convert_for_assignment (loc, ploc, type,
3497 val, origtype, ic_argpass,
3498 npc, fundecl, function,
3499 parmnum + 1);
3500
3501 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3502 && INTEGRAL_TYPE_P (type)
3503 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3504 parmval = default_conversion (parmval);
3505 }
3506 }
3507 else if (promote_float_arg)
3508 {
3509 if (type_generic)
3510 parmval = val;
3511 else
3512 {
3513 /* Convert `float' to `double'. */
3514 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3515 warning_at (ploc, OPT_Wdouble_promotion,
3516 "implicit conversion from %qT to %qT when passing "
3517 "argument to function",
3518 valtype, double_type_node);
3519 parmval = convert (double_type_node, val);
3520 }
3521 }
3522 else if ((excess_precision && !type_generic)
3523 || (type_generic_overflow_p && parmnum == 2))
3524 /* A "double" argument with excess precision being passed
3525 without a prototype or in variable arguments.
3526 The last argument of __builtin_*_overflow_p should not be
3527 promoted. */
3528 parmval = convert (valtype, val);
3529 else if ((invalid_func_diag =
3530 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3531 {
3532 error (invalid_func_diag);
3533 return -1;
3534 }
3535 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3536 {
3537 return -1;
3538 }
3539 else
3540 /* Convert `short' and `char' to full-size `int'. */
3541 parmval = default_conversion (val);
3542
3543 (*values)[parmnum] = parmval;
3544 if (parmval == error_mark_node)
3545 error_args = true;
3546
3547 if (typetail)
3548 typetail = TREE_CHAIN (typetail);
3549 }
3550
3551 gcc_assert (parmnum == vec_safe_length (values));
3552
3553 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3554 {
3555 error_at (loc, "too few arguments to function %qE", function);
3556 inform_declaration (fundecl);
3557 return -1;
3558 }
3559
3560 return error_args ? -1 : (int) parmnum;
3561 }
3562
3563 /* This is the entry point used by the parser to build unary operators
3564 in the input. CODE, a tree_code, specifies the unary operator, and
3565 ARG is the operand. For unary plus, the C parser currently uses
3566 CONVERT_EXPR for code.
3567
3568 LOC is the location to use for the tree generated.
3569 */
3570
3571 struct c_expr
parser_build_unary_op(location_t loc,enum tree_code code,struct c_expr arg)3572 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3573 {
3574 struct c_expr result;
3575
3576 result.original_code = code;
3577 result.original_type = NULL;
3578
3579 if (reject_gcc_builtin (arg.value))
3580 {
3581 result.value = error_mark_node;
3582 }
3583 else
3584 {
3585 result.value = build_unary_op (loc, code, arg.value, false);
3586
3587 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3588 overflow_warning (loc, result.value, arg.value);
3589 }
3590
3591 /* We are typically called when parsing a prefix token at LOC acting on
3592 ARG. Reflect this by updating the source range of the result to
3593 start at LOC and end at the end of ARG. */
3594 set_c_expr_source_range (&result,
3595 loc, arg.get_finish ());
3596
3597 return result;
3598 }
3599
3600 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3601
3602 static bool
char_type_p(tree type)3603 char_type_p (tree type)
3604 {
3605 return (type == char_type_node
3606 || type == unsigned_char_type_node
3607 || type == signed_char_type_node
3608 || type == char16_type_node
3609 || type == char32_type_node);
3610 }
3611
3612 /* This is the entry point used by the parser to build binary operators
3613 in the input. CODE, a tree_code, specifies the binary operator, and
3614 ARG1 and ARG2 are the operands. In addition to constructing the
3615 expression, we check for operands that were written with other binary
3616 operators in a way that is likely to confuse the user.
3617
3618 LOCATION is the location of the binary operator. */
3619
3620 struct c_expr
parser_build_binary_op(location_t location,enum tree_code code,struct c_expr arg1,struct c_expr arg2)3621 parser_build_binary_op (location_t location, enum tree_code code,
3622 struct c_expr arg1, struct c_expr arg2)
3623 {
3624 struct c_expr result;
3625
3626 enum tree_code code1 = arg1.original_code;
3627 enum tree_code code2 = arg2.original_code;
3628 tree type1 = (arg1.original_type
3629 ? arg1.original_type
3630 : TREE_TYPE (arg1.value));
3631 tree type2 = (arg2.original_type
3632 ? arg2.original_type
3633 : TREE_TYPE (arg2.value));
3634
3635 result.value = build_binary_op (location, code,
3636 arg1.value, arg2.value, true);
3637 result.original_code = code;
3638 result.original_type = NULL;
3639
3640 if (TREE_CODE (result.value) == ERROR_MARK)
3641 {
3642 set_c_expr_source_range (&result,
3643 arg1.get_start (),
3644 arg2.get_finish ());
3645 return result;
3646 }
3647
3648 if (location != UNKNOWN_LOCATION)
3649 protected_set_expr_location (result.value, location);
3650
3651 set_c_expr_source_range (&result,
3652 arg1.get_start (),
3653 arg2.get_finish ());
3654
3655 /* Check for cases such as x+y<<z which users are likely
3656 to misinterpret. */
3657 if (warn_parentheses)
3658 warn_about_parentheses (location, code, code1, arg1.value, code2,
3659 arg2.value);
3660
3661 if (warn_logical_op)
3662 warn_logical_operator (location, code, TREE_TYPE (result.value),
3663 code1, arg1.value, code2, arg2.value);
3664
3665 if (warn_tautological_compare)
3666 {
3667 tree lhs = arg1.value;
3668 tree rhs = arg2.value;
3669 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3670 {
3671 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3672 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3673 lhs = NULL_TREE;
3674 else
3675 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3676 }
3677 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3678 {
3679 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3680 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3681 rhs = NULL_TREE;
3682 else
3683 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3684 }
3685 if (lhs != NULL_TREE && rhs != NULL_TREE)
3686 warn_tautological_cmp (location, code, lhs, rhs);
3687 }
3688
3689 if (warn_logical_not_paren
3690 && TREE_CODE_CLASS (code) == tcc_comparison
3691 && code1 == TRUTH_NOT_EXPR
3692 && code2 != TRUTH_NOT_EXPR
3693 /* Avoid warning for !!x == y. */
3694 && (TREE_CODE (arg1.value) != NE_EXPR
3695 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3696 {
3697 /* Avoid warning for !b == y where b has _Bool type. */
3698 tree t = integer_zero_node;
3699 if (TREE_CODE (arg1.value) == EQ_EXPR
3700 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3701 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3702 {
3703 t = TREE_OPERAND (arg1.value, 0);
3704 do
3705 {
3706 if (TREE_TYPE (t) != integer_type_node)
3707 break;
3708 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3709 t = C_MAYBE_CONST_EXPR_EXPR (t);
3710 else if (CONVERT_EXPR_P (t))
3711 t = TREE_OPERAND (t, 0);
3712 else
3713 break;
3714 }
3715 while (1);
3716 }
3717 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3718 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3719 }
3720
3721 /* Warn about comparisons against string literals, with the exception
3722 of testing for equality or inequality of a string literal with NULL. */
3723 if (code == EQ_EXPR || code == NE_EXPR)
3724 {
3725 if ((code1 == STRING_CST
3726 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3727 || (code2 == STRING_CST
3728 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3729 warning_at (location, OPT_Waddress,
3730 "comparison with string literal results in unspecified behavior");
3731 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3732 if (POINTER_TYPE_P (type1)
3733 && null_pointer_constant_p (arg2.value)
3734 && char_type_p (type2)
3735 && warning_at (location, OPT_Wpointer_compare,
3736 "comparison between pointer and zero character "
3737 "constant"))
3738 inform (arg1.get_start (), "did you mean to dereference the pointer?");
3739 else if (POINTER_TYPE_P (type2)
3740 && null_pointer_constant_p (arg1.value)
3741 && char_type_p (type1)
3742 && warning_at (location, OPT_Wpointer_compare,
3743 "comparison between pointer and zero character "
3744 "constant"))
3745 inform (arg2.get_start (), "did you mean to dereference the pointer?");
3746 }
3747 else if (TREE_CODE_CLASS (code) == tcc_comparison
3748 && (code1 == STRING_CST || code2 == STRING_CST))
3749 warning_at (location, OPT_Waddress,
3750 "comparison with string literal results in unspecified behavior");
3751
3752 if (TREE_OVERFLOW_P (result.value)
3753 && !TREE_OVERFLOW_P (arg1.value)
3754 && !TREE_OVERFLOW_P (arg2.value))
3755 overflow_warning (location, result.value);
3756
3757 /* Warn about comparisons of different enum types. */
3758 if (warn_enum_compare
3759 && TREE_CODE_CLASS (code) == tcc_comparison
3760 && TREE_CODE (type1) == ENUMERAL_TYPE
3761 && TREE_CODE (type2) == ENUMERAL_TYPE
3762 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3763 warning_at (location, OPT_Wenum_compare,
3764 "comparison between %qT and %qT",
3765 type1, type2);
3766
3767 return result;
3768 }
3769
3770 /* Return a tree for the difference of pointers OP0 and OP1.
3771 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3772 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3773
3774 static tree
pointer_diff(location_t loc,tree op0,tree op1,tree * instrument_expr)3775 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3776 {
3777 tree restype = ptrdiff_type_node;
3778 tree result, inttype;
3779
3780 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3781 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3782 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3783 tree orig_op1 = op1;
3784
3785 /* If the operands point into different address spaces, we need to
3786 explicitly convert them to pointers into the common address space
3787 before we can subtract the numerical address values. */
3788 if (as0 != as1)
3789 {
3790 addr_space_t as_common;
3791 tree common_type;
3792
3793 /* Determine the common superset address space. This is guaranteed
3794 to exist because the caller verified that comp_target_types
3795 returned non-zero. */
3796 if (!addr_space_superset (as0, as1, &as_common))
3797 gcc_unreachable ();
3798
3799 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3800 op0 = convert (common_type, op0);
3801 op1 = convert (common_type, op1);
3802 }
3803
3804 /* Determine integer type result of the subtraction. This will usually
3805 be the same as the result type (ptrdiff_t), but may need to be a wider
3806 type if pointers for the address space are wider than ptrdiff_t. */
3807 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3808 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3809 else
3810 inttype = restype;
3811
3812 if (TREE_CODE (target_type) == VOID_TYPE)
3813 pedwarn (loc, OPT_Wpointer_arith,
3814 "pointer of type %<void *%> used in subtraction");
3815 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3816 pedwarn (loc, OPT_Wpointer_arith,
3817 "pointer to a function used in subtraction");
3818
3819 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
3820 {
3821 gcc_assert (current_function_decl != NULL_TREE);
3822
3823 op0 = save_expr (op0);
3824 op1 = save_expr (op1);
3825
3826 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
3827 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
3828 }
3829
3830 /* First do the subtraction, then build the divide operator
3831 and only convert at the very end.
3832 Do not do default conversions in case restype is a short type. */
3833
3834 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
3835 pointers. If some platform cannot provide that, or has a larger
3836 ptrdiff_type to support differences larger than half the address
3837 space, cast the pointers to some larger integer type and do the
3838 computations in that type. */
3839 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
3840 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
3841 convert (inttype, op1), false);
3842 else
3843 {
3844 /* Cast away qualifiers. */
3845 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
3846 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
3847 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
3848 }
3849
3850 /* This generates an error if op1 is pointer to incomplete type. */
3851 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3852 error_at (loc, "arithmetic on pointer to an incomplete type");
3853
3854 op1 = c_size_in_bytes (target_type);
3855
3856 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3857 error_at (loc, "arithmetic on pointer to an empty aggregate");
3858
3859 /* Divide by the size, in easiest possible way. */
3860 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3861 op0, convert (inttype, op1));
3862
3863 /* Convert to final result type if necessary. */
3864 return convert (restype, result);
3865 }
3866
3867 /* Expand atomic compound assignments into an appropriate sequence as
3868 specified by the C11 standard section 6.5.16.2.
3869
3870 _Atomic T1 E1
3871 T2 E2
3872 E1 op= E2
3873
3874 This sequence is used for all types for which these operations are
3875 supported.
3876
3877 In addition, built-in versions of the 'fe' prefixed routines may
3878 need to be invoked for floating point (real, complex or vector) when
3879 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3880
3881 T1 newval;
3882 T1 old;
3883 T1 *addr
3884 T2 val
3885 fenv_t fenv
3886
3887 addr = &E1;
3888 val = (E2);
3889 __atomic_load (addr, &old, SEQ_CST);
3890 feholdexcept (&fenv);
3891 loop:
3892 newval = old op val;
3893 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3894 SEQ_CST))
3895 goto done;
3896 feclearexcept (FE_ALL_EXCEPT);
3897 goto loop:
3898 done:
3899 feupdateenv (&fenv);
3900
3901 The compiler will issue the __atomic_fetch_* built-in when possible,
3902 otherwise it will generate the generic form of the atomic operations.
3903 This requires temp(s) and has their address taken. The atomic processing
3904 is smart enough to figure out when the size of an object can utilize
3905 a lock-free version, and convert the built-in call to the appropriate
3906 lock-free routine. The optimizers will then dispose of any temps that
3907 are no longer required, and lock-free implementations are utilized as
3908 long as there is target support for the required size.
3909
3910 If the operator is NOP_EXPR, then this is a simple assignment, and
3911 an __atomic_store is issued to perform the assignment rather than
3912 the above loop. */
3913
3914 /* Build an atomic assignment at LOC, expanding into the proper
3915 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3916 the result of the operation, unless RETURN_OLD_P, in which case
3917 return the old value of LHS (this is only for postincrement and
3918 postdecrement). */
3919
3920 static tree
build_atomic_assign(location_t loc,tree lhs,enum tree_code modifycode,tree rhs,bool return_old_p)3921 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3922 tree rhs, bool return_old_p)
3923 {
3924 tree fndecl, func_call;
3925 vec<tree, va_gc> *params;
3926 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3927 tree old, old_addr;
3928 tree compound_stmt;
3929 tree stmt, goto_stmt;
3930 tree loop_label, loop_decl, done_label, done_decl;
3931
3932 tree lhs_type = TREE_TYPE (lhs);
3933 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
3934 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3935 tree rhs_semantic_type = TREE_TYPE (rhs);
3936 tree nonatomic_rhs_semantic_type;
3937 tree rhs_type;
3938
3939 gcc_assert (TYPE_ATOMIC (lhs_type));
3940
3941 if (return_old_p)
3942 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3943
3944 /* Allocate enough vector items for a compare_exchange. */
3945 vec_alloc (params, 6);
3946
3947 /* Create a compound statement to hold the sequence of statements
3948 with a loop. */
3949 compound_stmt = c_begin_compound_stmt (false);
3950
3951 /* Remove any excess precision (which is only present here in the
3952 case of compound assignments). */
3953 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
3954 {
3955 gcc_assert (modifycode != NOP_EXPR);
3956 rhs = TREE_OPERAND (rhs, 0);
3957 }
3958 rhs_type = TREE_TYPE (rhs);
3959
3960 /* Fold the RHS if it hasn't already been folded. */
3961 if (modifycode != NOP_EXPR)
3962 rhs = c_fully_fold (rhs, false, NULL);
3963
3964 /* Remove the qualifiers for the rest of the expressions and create
3965 the VAL temp variable to hold the RHS. */
3966 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3967 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3968 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
3969 TYPE_UNQUALIFIED);
3970 val = create_tmp_var_raw (nonatomic_rhs_type);
3971 TREE_ADDRESSABLE (val) = 1;
3972 TREE_NO_WARNING (val) = 1;
3973 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3974 NULL_TREE);
3975 SET_EXPR_LOCATION (rhs, loc);
3976 add_stmt (rhs);
3977
3978 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3979 an atomic_store. */
3980 if (modifycode == NOP_EXPR)
3981 {
3982 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3983 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
3984 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3985 params->quick_push (lhs_addr);
3986 params->quick_push (rhs);
3987 params->quick_push (seq_cst);
3988 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3989 add_stmt (func_call);
3990
3991 /* Finish the compound statement. */
3992 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3993
3994 /* VAL is the value which was stored, return a COMPOUND_STMT of
3995 the statement and that value. */
3996 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3997 }
3998
3999 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4000 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4001 isn't applicable for such builtins. ??? Do we want to handle enums? */
4002 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4003 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4004 {
4005 built_in_function fncode;
4006 switch (modifycode)
4007 {
4008 case PLUS_EXPR:
4009 case POINTER_PLUS_EXPR:
4010 fncode = (return_old_p
4011 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4012 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4013 break;
4014 case MINUS_EXPR:
4015 fncode = (return_old_p
4016 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4017 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4018 break;
4019 case BIT_AND_EXPR:
4020 fncode = (return_old_p
4021 ? BUILT_IN_ATOMIC_FETCH_AND_N
4022 : BUILT_IN_ATOMIC_AND_FETCH_N);
4023 break;
4024 case BIT_IOR_EXPR:
4025 fncode = (return_old_p
4026 ? BUILT_IN_ATOMIC_FETCH_OR_N
4027 : BUILT_IN_ATOMIC_OR_FETCH_N);
4028 break;
4029 case BIT_XOR_EXPR:
4030 fncode = (return_old_p
4031 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4032 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4033 break;
4034 default:
4035 goto cas_loop;
4036 }
4037
4038 /* We can only use "_1" through "_16" variants of the atomic fetch
4039 built-ins. */
4040 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4041 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4042 goto cas_loop;
4043
4044 /* If this is a pointer type, we need to multiply by the size of
4045 the pointer target type. */
4046 if (POINTER_TYPE_P (lhs_type))
4047 {
4048 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4049 /* ??? This would introduce -Wdiscarded-qualifiers
4050 warning: __atomic_fetch_* expect volatile void *
4051 type as the first argument. (Assignments between
4052 atomic and non-atomic objects are OK.) */
4053 || TYPE_RESTRICT (lhs_type))
4054 goto cas_loop;
4055 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4056 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4057 convert (ptrdiff_type_node, rhs),
4058 convert (ptrdiff_type_node, sz));
4059 }
4060
4061 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4062 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4063 fndecl = builtin_decl_explicit (fncode);
4064 params->quick_push (lhs_addr);
4065 params->quick_push (rhs);
4066 params->quick_push (seq_cst);
4067 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4068
4069 newval = create_tmp_var_raw (nonatomic_lhs_type);
4070 TREE_ADDRESSABLE (newval) = 1;
4071 TREE_NO_WARNING (newval) = 1;
4072 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4073 NULL_TREE, NULL_TREE);
4074 SET_EXPR_LOCATION (rhs, loc);
4075 add_stmt (rhs);
4076
4077 /* Finish the compound statement. */
4078 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4079
4080 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4081 the statement and that value. */
4082 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4083 }
4084
4085 cas_loop:
4086 /* Create the variables and labels required for the op= form. */
4087 old = create_tmp_var_raw (nonatomic_lhs_type);
4088 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4089 TREE_ADDRESSABLE (old) = 1;
4090 TREE_NO_WARNING (old) = 1;
4091
4092 newval = create_tmp_var_raw (nonatomic_lhs_type);
4093 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4094 TREE_ADDRESSABLE (newval) = 1;
4095 TREE_NO_WARNING (newval) = 1;
4096
4097 loop_decl = create_artificial_label (loc);
4098 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4099
4100 done_decl = create_artificial_label (loc);
4101 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4102
4103 /* __atomic_load (addr, &old, SEQ_CST). */
4104 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4105 params->quick_push (lhs_addr);
4106 params->quick_push (old_addr);
4107 params->quick_push (seq_cst);
4108 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4109 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4110 NULL_TREE);
4111 add_stmt (old);
4112 params->truncate (0);
4113
4114 /* Create the expressions for floating-point environment
4115 manipulation, if required. */
4116 bool need_fenv = (flag_trapping_math
4117 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4118 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4119 if (need_fenv)
4120 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4121
4122 if (hold_call)
4123 add_stmt (hold_call);
4124
4125 /* loop: */
4126 add_stmt (loop_label);
4127
4128 /* newval = old + val; */
4129 if (rhs_type != rhs_semantic_type)
4130 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4131 rhs = build_binary_op (loc, modifycode, old, val, true);
4132 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4133 {
4134 tree eptype = TREE_TYPE (rhs);
4135 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4136 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4137 }
4138 else
4139 rhs = c_fully_fold (rhs, false, NULL);
4140 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4141 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4142 NULL_TREE, 0);
4143 if (rhs != error_mark_node)
4144 {
4145 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4146 NULL_TREE);
4147 SET_EXPR_LOCATION (rhs, loc);
4148 add_stmt (rhs);
4149 }
4150
4151 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4152 goto done; */
4153 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4154 params->quick_push (lhs_addr);
4155 params->quick_push (old_addr);
4156 params->quick_push (newval_addr);
4157 params->quick_push (integer_zero_node);
4158 params->quick_push (seq_cst);
4159 params->quick_push (seq_cst);
4160 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4161
4162 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4163 SET_EXPR_LOCATION (goto_stmt, loc);
4164
4165 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4166 SET_EXPR_LOCATION (stmt, loc);
4167 add_stmt (stmt);
4168
4169 if (clear_call)
4170 add_stmt (clear_call);
4171
4172 /* goto loop; */
4173 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4174 SET_EXPR_LOCATION (goto_stmt, loc);
4175 add_stmt (goto_stmt);
4176
4177 /* done: */
4178 add_stmt (done_label);
4179
4180 if (update_call)
4181 add_stmt (update_call);
4182
4183 /* Finish the compound statement. */
4184 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4185
4186 /* NEWVAL is the value that was successfully stored, return a
4187 COMPOUND_EXPR of the statement and the appropriate value. */
4188 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4189 return_old_p ? old : newval);
4190 }
4191
4192 /* Construct and perhaps optimize a tree representation
4193 for a unary operation. CODE, a tree_code, specifies the operation
4194 and XARG is the operand.
4195 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4196 promotions (such as from short to int).
4197 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4198 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4199 to pointers in C99.
4200
4201 LOCATION is the location of the operator. */
4202
4203 tree
build_unary_op(location_t location,enum tree_code code,tree xarg,bool noconvert)4204 build_unary_op (location_t location, enum tree_code code, tree xarg,
4205 bool noconvert)
4206 {
4207 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4208 tree arg = xarg;
4209 tree argtype = NULL_TREE;
4210 enum tree_code typecode;
4211 tree val;
4212 tree ret = error_mark_node;
4213 tree eptype = NULL_TREE;
4214 const char *invalid_op_diag;
4215 bool int_operands;
4216
4217 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4218 if (int_operands)
4219 arg = remove_c_maybe_const_expr (arg);
4220
4221 if (code != ADDR_EXPR)
4222 arg = require_complete_type (location, arg);
4223
4224 typecode = TREE_CODE (TREE_TYPE (arg));
4225 if (typecode == ERROR_MARK)
4226 return error_mark_node;
4227 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4228 typecode = INTEGER_TYPE;
4229
4230 if ((invalid_op_diag
4231 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4232 {
4233 error_at (location, invalid_op_diag);
4234 return error_mark_node;
4235 }
4236
4237 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4238 {
4239 eptype = TREE_TYPE (arg);
4240 arg = TREE_OPERAND (arg, 0);
4241 }
4242
4243 switch (code)
4244 {
4245 case CONVERT_EXPR:
4246 /* This is used for unary plus, because a CONVERT_EXPR
4247 is enough to prevent anybody from looking inside for
4248 associativity, but won't generate any code. */
4249 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4250 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4251 || typecode == VECTOR_TYPE))
4252 {
4253 error_at (location, "wrong type argument to unary plus");
4254 return error_mark_node;
4255 }
4256 else if (!noconvert)
4257 arg = default_conversion (arg);
4258 arg = non_lvalue_loc (location, arg);
4259 break;
4260
4261 case NEGATE_EXPR:
4262 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4263 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4264 || typecode == VECTOR_TYPE))
4265 {
4266 error_at (location, "wrong type argument to unary minus");
4267 return error_mark_node;
4268 }
4269 else if (!noconvert)
4270 arg = default_conversion (arg);
4271 break;
4272
4273 case BIT_NOT_EXPR:
4274 /* ~ works on integer types and non float vectors. */
4275 if (typecode == INTEGER_TYPE
4276 || (typecode == VECTOR_TYPE
4277 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4278 {
4279 tree e = arg;
4280
4281 /* Warn if the expression has boolean value. */
4282 while (TREE_CODE (e) == COMPOUND_EXPR)
4283 e = TREE_OPERAND (e, 1);
4284
4285 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4286 || truth_value_p (TREE_CODE (e)))
4287 && warning_at (location, OPT_Wbool_operation,
4288 "%<~%> on a boolean expression"))
4289 {
4290 gcc_rich_location richloc (location);
4291 richloc.add_fixit_insert_before (location, "!");
4292 inform (&richloc, "did you mean to use logical not?");
4293 }
4294 if (!noconvert)
4295 arg = default_conversion (arg);
4296 }
4297 else if (typecode == COMPLEX_TYPE)
4298 {
4299 code = CONJ_EXPR;
4300 pedwarn (location, OPT_Wpedantic,
4301 "ISO C does not support %<~%> for complex conjugation");
4302 if (!noconvert)
4303 arg = default_conversion (arg);
4304 }
4305 else
4306 {
4307 error_at (location, "wrong type argument to bit-complement");
4308 return error_mark_node;
4309 }
4310 break;
4311
4312 case ABS_EXPR:
4313 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4314 {
4315 error_at (location, "wrong type argument to abs");
4316 return error_mark_node;
4317 }
4318 else if (!noconvert)
4319 arg = default_conversion (arg);
4320 break;
4321
4322 case CONJ_EXPR:
4323 /* Conjugating a real value is a no-op, but allow it anyway. */
4324 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4325 || typecode == COMPLEX_TYPE))
4326 {
4327 error_at (location, "wrong type argument to conjugation");
4328 return error_mark_node;
4329 }
4330 else if (!noconvert)
4331 arg = default_conversion (arg);
4332 break;
4333
4334 case TRUTH_NOT_EXPR:
4335 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4336 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4337 && typecode != COMPLEX_TYPE)
4338 {
4339 error_at (location,
4340 "wrong type argument to unary exclamation mark");
4341 return error_mark_node;
4342 }
4343 if (int_operands)
4344 {
4345 arg = c_objc_common_truthvalue_conversion (location, xarg);
4346 arg = remove_c_maybe_const_expr (arg);
4347 }
4348 else
4349 arg = c_objc_common_truthvalue_conversion (location, arg);
4350 ret = invert_truthvalue_loc (location, arg);
4351 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4352 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4353 location = EXPR_LOCATION (ret);
4354 goto return_build_unary_op;
4355
4356 case REALPART_EXPR:
4357 case IMAGPART_EXPR:
4358 ret = build_real_imag_expr (location, code, arg);
4359 if (ret == error_mark_node)
4360 return error_mark_node;
4361 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4362 eptype = TREE_TYPE (eptype);
4363 goto return_build_unary_op;
4364
4365 case PREINCREMENT_EXPR:
4366 case POSTINCREMENT_EXPR:
4367 case PREDECREMENT_EXPR:
4368 case POSTDECREMENT_EXPR:
4369
4370 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4371 {
4372 tree inner = build_unary_op (location, code,
4373 C_MAYBE_CONST_EXPR_EXPR (arg),
4374 noconvert);
4375 if (inner == error_mark_node)
4376 return error_mark_node;
4377 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4378 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4379 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4380 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4381 goto return_build_unary_op;
4382 }
4383
4384 /* Complain about anything that is not a true lvalue. In
4385 Objective-C, skip this check for property_refs. */
4386 if (!objc_is_property_ref (arg)
4387 && !lvalue_or_else (location,
4388 arg, ((code == PREINCREMENT_EXPR
4389 || code == POSTINCREMENT_EXPR)
4390 ? lv_increment
4391 : lv_decrement)))
4392 return error_mark_node;
4393
4394 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4395 {
4396 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4397 warning_at (location, OPT_Wc___compat,
4398 "increment of enumeration value is invalid in C++");
4399 else
4400 warning_at (location, OPT_Wc___compat,
4401 "decrement of enumeration value is invalid in C++");
4402 }
4403
4404 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4405 {
4406 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4407 warning_at (location, OPT_Wbool_operation,
4408 "increment of a boolean expression");
4409 else
4410 warning_at (location, OPT_Wbool_operation,
4411 "decrement of a boolean expression");
4412 }
4413
4414 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4415 arg = c_fully_fold (arg, false, NULL, true);
4416
4417 bool atomic_op;
4418 atomic_op = really_atomic_lvalue (arg);
4419
4420 /* Increment or decrement the real part of the value,
4421 and don't change the imaginary part. */
4422 if (typecode == COMPLEX_TYPE)
4423 {
4424 tree real, imag;
4425
4426 pedwarn (location, OPT_Wpedantic,
4427 "ISO C does not support %<++%> and %<--%> on complex types");
4428
4429 if (!atomic_op)
4430 {
4431 arg = stabilize_reference (arg);
4432 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4433 true);
4434 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4435 true);
4436 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4437 if (real == error_mark_node || imag == error_mark_node)
4438 return error_mark_node;
4439 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4440 real, imag);
4441 goto return_build_unary_op;
4442 }
4443 }
4444
4445 /* Report invalid types. */
4446
4447 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4448 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4449 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4450 {
4451 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4452 error_at (location, "wrong type argument to increment");
4453 else
4454 error_at (location, "wrong type argument to decrement");
4455
4456 return error_mark_node;
4457 }
4458
4459 {
4460 tree inc;
4461
4462 argtype = TREE_TYPE (arg);
4463
4464 /* Compute the increment. */
4465
4466 if (typecode == POINTER_TYPE)
4467 {
4468 /* If pointer target is an incomplete type,
4469 we just cannot know how to do the arithmetic. */
4470 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4471 {
4472 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4473 error_at (location,
4474 "increment of pointer to an incomplete type %qT",
4475 TREE_TYPE (argtype));
4476 else
4477 error_at (location,
4478 "decrement of pointer to an incomplete type %qT",
4479 TREE_TYPE (argtype));
4480 }
4481 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4482 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4483 {
4484 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4485 pedwarn (location, OPT_Wpointer_arith,
4486 "wrong type argument to increment");
4487 else
4488 pedwarn (location, OPT_Wpointer_arith,
4489 "wrong type argument to decrement");
4490 }
4491
4492 inc = c_size_in_bytes (TREE_TYPE (argtype));
4493 inc = convert_to_ptrofftype_loc (location, inc);
4494 }
4495 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4496 {
4497 /* For signed fract types, we invert ++ to -- or
4498 -- to ++, and change inc from 1 to -1, because
4499 it is not possible to represent 1 in signed fract constants.
4500 For unsigned fract types, the result always overflows and
4501 we get an undefined (original) or the maximum value. */
4502 if (code == PREINCREMENT_EXPR)
4503 code = PREDECREMENT_EXPR;
4504 else if (code == PREDECREMENT_EXPR)
4505 code = PREINCREMENT_EXPR;
4506 else if (code == POSTINCREMENT_EXPR)
4507 code = POSTDECREMENT_EXPR;
4508 else /* code == POSTDECREMENT_EXPR */
4509 code = POSTINCREMENT_EXPR;
4510
4511 inc = integer_minus_one_node;
4512 inc = convert (argtype, inc);
4513 }
4514 else
4515 {
4516 inc = VECTOR_TYPE_P (argtype)
4517 ? build_one_cst (argtype)
4518 : integer_one_node;
4519 inc = convert (argtype, inc);
4520 }
4521
4522 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4523 need to ask Objective-C to build the increment or decrement
4524 expression for it. */
4525 if (objc_is_property_ref (arg))
4526 return objc_build_incr_expr_for_property_ref (location, code,
4527 arg, inc);
4528
4529 /* Report a read-only lvalue. */
4530 if (TYPE_READONLY (argtype))
4531 {
4532 readonly_error (location, arg,
4533 ((code == PREINCREMENT_EXPR
4534 || code == POSTINCREMENT_EXPR)
4535 ? lv_increment : lv_decrement));
4536 return error_mark_node;
4537 }
4538 else if (TREE_READONLY (arg))
4539 readonly_warning (arg,
4540 ((code == PREINCREMENT_EXPR
4541 || code == POSTINCREMENT_EXPR)
4542 ? lv_increment : lv_decrement));
4543
4544 /* If the argument is atomic, use the special code sequences for
4545 atomic compound assignment. */
4546 if (atomic_op)
4547 {
4548 arg = stabilize_reference (arg);
4549 ret = build_atomic_assign (location, arg,
4550 ((code == PREINCREMENT_EXPR
4551 || code == POSTINCREMENT_EXPR)
4552 ? PLUS_EXPR
4553 : MINUS_EXPR),
4554 (FRACT_MODE_P (TYPE_MODE (argtype))
4555 ? inc
4556 : integer_one_node),
4557 (code == POSTINCREMENT_EXPR
4558 || code == POSTDECREMENT_EXPR));
4559 goto return_build_unary_op;
4560 }
4561
4562 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4563 val = boolean_increment (code, arg);
4564 else
4565 val = build2 (code, TREE_TYPE (arg), arg, inc);
4566 TREE_SIDE_EFFECTS (val) = 1;
4567 if (TREE_CODE (val) != code)
4568 TREE_NO_WARNING (val) = 1;
4569 ret = val;
4570 goto return_build_unary_op;
4571 }
4572
4573 case ADDR_EXPR:
4574 /* Note that this operation never does default_conversion. */
4575
4576 /* The operand of unary '&' must be an lvalue (which excludes
4577 expressions of type void), or, in C99, the result of a [] or
4578 unary '*' operator. */
4579 if (VOID_TYPE_P (TREE_TYPE (arg))
4580 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4581 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4582 pedwarn (location, 0, "taking address of expression of type %<void%>");
4583
4584 /* Let &* cancel out to simplify resulting code. */
4585 if (INDIRECT_REF_P (arg))
4586 {
4587 /* Don't let this be an lvalue. */
4588 if (lvalue_p (TREE_OPERAND (arg, 0)))
4589 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4590 ret = TREE_OPERAND (arg, 0);
4591 goto return_build_unary_op;
4592 }
4593
4594 /* Anything not already handled and not a true memory reference
4595 or a non-lvalue array is an error. */
4596 if (typecode != FUNCTION_TYPE && !noconvert
4597 && !lvalue_or_else (location, arg, lv_addressof))
4598 return error_mark_node;
4599
4600 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4601 folding later. */
4602 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4603 {
4604 tree inner = build_unary_op (location, code,
4605 C_MAYBE_CONST_EXPR_EXPR (arg),
4606 noconvert);
4607 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4608 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4609 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4610 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4611 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4612 goto return_build_unary_op;
4613 }
4614
4615 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4616 argtype = TREE_TYPE (arg);
4617
4618 /* If the lvalue is const or volatile, merge that into the type
4619 to which the address will point. This is only needed
4620 for function types. */
4621 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4622 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4623 && TREE_CODE (argtype) == FUNCTION_TYPE)
4624 {
4625 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4626 int quals = orig_quals;
4627
4628 if (TREE_READONLY (arg))
4629 quals |= TYPE_QUAL_CONST;
4630 if (TREE_THIS_VOLATILE (arg))
4631 quals |= TYPE_QUAL_VOLATILE;
4632
4633 argtype = c_build_qualified_type (argtype, quals);
4634 }
4635
4636 switch (TREE_CODE (arg))
4637 {
4638 case COMPONENT_REF:
4639 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4640 {
4641 error_at (location, "cannot take address of bit-field %qD",
4642 TREE_OPERAND (arg, 1));
4643 return error_mark_node;
4644 }
4645
4646 /* fall through */
4647
4648 case ARRAY_REF:
4649 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4650 {
4651 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4652 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4653 {
4654 error_at (location, "cannot take address of scalar with "
4655 "reverse storage order");
4656 return error_mark_node;
4657 }
4658
4659 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4660 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4661 warning_at (location, OPT_Wscalar_storage_order,
4662 "address of array with reverse scalar storage "
4663 "order requested");
4664 }
4665
4666 default:
4667 break;
4668 }
4669
4670 if (!c_mark_addressable (arg))
4671 return error_mark_node;
4672
4673 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4674 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4675
4676 argtype = build_pointer_type (argtype);
4677
4678 /* ??? Cope with user tricks that amount to offsetof. Delete this
4679 when we have proper support for integer constant expressions. */
4680 val = get_base_address (arg);
4681 if (val && INDIRECT_REF_P (val)
4682 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4683 {
4684 ret = fold_offsetof (arg, argtype);
4685 goto return_build_unary_op;
4686 }
4687
4688 val = build1 (ADDR_EXPR, argtype, arg);
4689
4690 ret = val;
4691 goto return_build_unary_op;
4692
4693 default:
4694 gcc_unreachable ();
4695 }
4696
4697 if (argtype == NULL_TREE)
4698 argtype = TREE_TYPE (arg);
4699 if (TREE_CODE (arg) == INTEGER_CST)
4700 ret = (require_constant_value
4701 ? fold_build1_initializer_loc (location, code, argtype, arg)
4702 : fold_build1_loc (location, code, argtype, arg));
4703 else
4704 ret = build1 (code, argtype, arg);
4705 return_build_unary_op:
4706 gcc_assert (ret != error_mark_node);
4707 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4708 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4709 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4710 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4711 ret = note_integer_operands (ret);
4712 if (eptype)
4713 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4714 protected_set_expr_location (ret, location);
4715 return ret;
4716 }
4717
4718 /* Return nonzero if REF is an lvalue valid for this language.
4719 Lvalues can be assigned, unless their type has TYPE_READONLY.
4720 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4721
4722 bool
lvalue_p(const_tree ref)4723 lvalue_p (const_tree ref)
4724 {
4725 const enum tree_code code = TREE_CODE (ref);
4726
4727 switch (code)
4728 {
4729 case REALPART_EXPR:
4730 case IMAGPART_EXPR:
4731 case COMPONENT_REF:
4732 return lvalue_p (TREE_OPERAND (ref, 0));
4733
4734 case C_MAYBE_CONST_EXPR:
4735 return lvalue_p (TREE_OPERAND (ref, 1));
4736
4737 case COMPOUND_LITERAL_EXPR:
4738 case STRING_CST:
4739 return true;
4740
4741 case INDIRECT_REF:
4742 case ARRAY_REF:
4743 case VAR_DECL:
4744 case PARM_DECL:
4745 case RESULT_DECL:
4746 case ERROR_MARK:
4747 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4748 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4749
4750 case BIND_EXPR:
4751 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4752
4753 default:
4754 return false;
4755 }
4756 }
4757
4758 /* Give a warning for storing in something that is read-only in GCC
4759 terms but not const in ISO C terms. */
4760
4761 static void
readonly_warning(tree arg,enum lvalue_use use)4762 readonly_warning (tree arg, enum lvalue_use use)
4763 {
4764 switch (use)
4765 {
4766 case lv_assign:
4767 warning (0, "assignment of read-only location %qE", arg);
4768 break;
4769 case lv_increment:
4770 warning (0, "increment of read-only location %qE", arg);
4771 break;
4772 case lv_decrement:
4773 warning (0, "decrement of read-only location %qE", arg);
4774 break;
4775 default:
4776 gcc_unreachable ();
4777 }
4778 return;
4779 }
4780
4781
4782 /* Return nonzero if REF is an lvalue valid for this language;
4783 otherwise, print an error message and return zero. USE says
4784 how the lvalue is being used and so selects the error message.
4785 LOCATION is the location at which any error should be reported. */
4786
4787 static int
lvalue_or_else(location_t loc,const_tree ref,enum lvalue_use use)4788 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4789 {
4790 int win = lvalue_p (ref);
4791
4792 if (!win)
4793 lvalue_error (loc, use);
4794
4795 return win;
4796 }
4797
4798 /* Mark EXP saying that we need to be able to take the
4799 address of it; it should not be allocated in a register.
4800 Returns true if successful. ARRAY_REF_P is true if this
4801 is for ARRAY_REF construction - in that case we don't want
4802 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4803 it is fine to use ARRAY_REFs for vector subscripts on vector
4804 register variables. */
4805
4806 bool
c_mark_addressable(tree exp,bool array_ref_p)4807 c_mark_addressable (tree exp, bool array_ref_p)
4808 {
4809 tree x = exp;
4810
4811 while (1)
4812 switch (TREE_CODE (x))
4813 {
4814 case VIEW_CONVERT_EXPR:
4815 if (array_ref_p
4816 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4817 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4818 return true;
4819 /* FALLTHRU */
4820 case COMPONENT_REF:
4821 case ADDR_EXPR:
4822 case ARRAY_REF:
4823 case REALPART_EXPR:
4824 case IMAGPART_EXPR:
4825 x = TREE_OPERAND (x, 0);
4826 break;
4827
4828 case COMPOUND_LITERAL_EXPR:
4829 case CONSTRUCTOR:
4830 TREE_ADDRESSABLE (x) = 1;
4831 return true;
4832
4833 case VAR_DECL:
4834 case CONST_DECL:
4835 case PARM_DECL:
4836 case RESULT_DECL:
4837 if (C_DECL_REGISTER (x)
4838 && DECL_NONLOCAL (x))
4839 {
4840 if (TREE_PUBLIC (x) || is_global_var (x))
4841 {
4842 error
4843 ("global register variable %qD used in nested function", x);
4844 return false;
4845 }
4846 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4847 }
4848 else if (C_DECL_REGISTER (x))
4849 {
4850 if (TREE_PUBLIC (x) || is_global_var (x))
4851 error ("address of global register variable %qD requested", x);
4852 else
4853 error ("address of register variable %qD requested", x);
4854 return false;
4855 }
4856
4857 /* FALLTHRU */
4858 case FUNCTION_DECL:
4859 TREE_ADDRESSABLE (x) = 1;
4860 /* FALLTHRU */
4861 default:
4862 return true;
4863 }
4864 }
4865
4866 /* Convert EXPR to TYPE, warning about conversion problems with
4867 constants. SEMANTIC_TYPE is the type this conversion would use
4868 without excess precision. If SEMANTIC_TYPE is NULL, this function
4869 is equivalent to convert_and_check. This function is a wrapper that
4870 handles conversions that may be different than
4871 the usual ones because of excess precision. */
4872
4873 static tree
ep_convert_and_check(location_t loc,tree type,tree expr,tree semantic_type)4874 ep_convert_and_check (location_t loc, tree type, tree expr,
4875 tree semantic_type)
4876 {
4877 if (TREE_TYPE (expr) == type)
4878 return expr;
4879
4880 /* For C11, integer conversions may have results with excess
4881 precision. */
4882 if (flag_isoc11 || !semantic_type)
4883 return convert_and_check (loc, type, expr);
4884
4885 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4886 && TREE_TYPE (expr) != semantic_type)
4887 {
4888 /* For integers, we need to check the real conversion, not
4889 the conversion to the excess precision type. */
4890 expr = convert_and_check (loc, semantic_type, expr);
4891 }
4892 /* Result type is the excess precision type, which should be
4893 large enough, so do not check. */
4894 return convert (type, expr);
4895 }
4896
4897 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4898 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4899 if folded to an integer constant then the unselected half may
4900 contain arbitrary operations not normally permitted in constant
4901 expressions. Set the location of the expression to LOC. */
4902
4903 tree
build_conditional_expr(location_t colon_loc,tree ifexp,bool ifexp_bcp,tree op1,tree op1_original_type,location_t op1_loc,tree op2,tree op2_original_type,location_t op2_loc)4904 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4905 tree op1, tree op1_original_type, location_t op1_loc,
4906 tree op2, tree op2_original_type, location_t op2_loc)
4907 {
4908 tree type1;
4909 tree type2;
4910 enum tree_code code1;
4911 enum tree_code code2;
4912 tree result_type = NULL;
4913 tree semantic_result_type = NULL;
4914 tree orig_op1 = op1, orig_op2 = op2;
4915 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4916 bool ifexp_int_operands;
4917 tree ret;
4918
4919 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4920 if (op1_int_operands)
4921 op1 = remove_c_maybe_const_expr (op1);
4922 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4923 if (op2_int_operands)
4924 op2 = remove_c_maybe_const_expr (op2);
4925 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4926 if (ifexp_int_operands)
4927 ifexp = remove_c_maybe_const_expr (ifexp);
4928
4929 /* Promote both alternatives. */
4930
4931 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4932 op1 = default_conversion (op1);
4933 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4934 op2 = default_conversion (op2);
4935
4936 if (TREE_CODE (ifexp) == ERROR_MARK
4937 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4938 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4939 return error_mark_node;
4940
4941 type1 = TREE_TYPE (op1);
4942 code1 = TREE_CODE (type1);
4943 type2 = TREE_TYPE (op2);
4944 code2 = TREE_CODE (type2);
4945
4946 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4947 return error_mark_node;
4948
4949 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4950 return error_mark_node;
4951
4952 /* C90 does not permit non-lvalue arrays in conditional expressions.
4953 In C99 they will be pointers by now. */
4954 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4955 {
4956 error_at (colon_loc, "non-lvalue array in conditional expression");
4957 return error_mark_node;
4958 }
4959
4960 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4961 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4962 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4963 || code1 == COMPLEX_TYPE)
4964 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4965 || code2 == COMPLEX_TYPE))
4966 {
4967 semantic_result_type = c_common_type (type1, type2);
4968 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4969 {
4970 op1 = TREE_OPERAND (op1, 0);
4971 type1 = TREE_TYPE (op1);
4972 gcc_assert (TREE_CODE (type1) == code1);
4973 }
4974 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4975 {
4976 op2 = TREE_OPERAND (op2, 0);
4977 type2 = TREE_TYPE (op2);
4978 gcc_assert (TREE_CODE (type2) == code2);
4979 }
4980 }
4981
4982 if (warn_cxx_compat)
4983 {
4984 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4985 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4986
4987 if (TREE_CODE (t1) == ENUMERAL_TYPE
4988 && TREE_CODE (t2) == ENUMERAL_TYPE
4989 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4990 warning_at (colon_loc, OPT_Wc___compat,
4991 ("different enum types in conditional is "
4992 "invalid in C++: %qT vs %qT"),
4993 t1, t2);
4994 }
4995
4996 /* Quickly detect the usual case where op1 and op2 have the same type
4997 after promotion. */
4998 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4999 {
5000 if (type1 == type2)
5001 result_type = type1;
5002 else
5003 result_type = TYPE_MAIN_VARIANT (type1);
5004 }
5005 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5006 || code1 == COMPLEX_TYPE)
5007 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5008 || code2 == COMPLEX_TYPE))
5009 {
5010 /* In C11, a conditional expression between a floating-point
5011 type and an integer type should convert the integer type to
5012 the evaluation format of the floating-point type, with
5013 possible excess precision. */
5014 tree eptype1 = type1;
5015 tree eptype2 = type2;
5016 if (flag_isoc11)
5017 {
5018 tree eptype;
5019 if (ANY_INTEGRAL_TYPE_P (type1)
5020 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5021 {
5022 eptype2 = eptype;
5023 if (!semantic_result_type)
5024 semantic_result_type = c_common_type (type1, type2);
5025 }
5026 else if (ANY_INTEGRAL_TYPE_P (type2)
5027 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5028 {
5029 eptype1 = eptype;
5030 if (!semantic_result_type)
5031 semantic_result_type = c_common_type (type1, type2);
5032 }
5033 }
5034 result_type = c_common_type (eptype1, eptype2);
5035 if (result_type == error_mark_node)
5036 return error_mark_node;
5037 do_warn_double_promotion (result_type, type1, type2,
5038 "implicit conversion from %qT to %qT to "
5039 "match other result of conditional",
5040 colon_loc);
5041
5042 /* If -Wsign-compare, warn here if type1 and type2 have
5043 different signedness. We'll promote the signed to unsigned
5044 and later code won't know it used to be different.
5045 Do this check on the original types, so that explicit casts
5046 will be considered, but default promotions won't. */
5047 if (c_inhibit_evaluation_warnings == 0)
5048 {
5049 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5050 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5051
5052 if (unsigned_op1 ^ unsigned_op2)
5053 {
5054 bool ovf;
5055
5056 /* Do not warn if the result type is signed, since the
5057 signed type will only be chosen if it can represent
5058 all the values of the unsigned type. */
5059 if (!TYPE_UNSIGNED (result_type))
5060 /* OK */;
5061 else
5062 {
5063 bool op1_maybe_const = true;
5064 bool op2_maybe_const = true;
5065
5066 /* Do not warn if the signed quantity is an
5067 unsuffixed integer literal (or some static
5068 constant expression involving such literals) and
5069 it is non-negative. This warning requires the
5070 operands to be folded for best results, so do
5071 that folding in this case even without
5072 warn_sign_compare to avoid warning options
5073 possibly affecting code generation. */
5074 c_inhibit_evaluation_warnings
5075 += (ifexp == truthvalue_false_node);
5076 op1 = c_fully_fold (op1, require_constant_value,
5077 &op1_maybe_const);
5078 c_inhibit_evaluation_warnings
5079 -= (ifexp == truthvalue_false_node);
5080
5081 c_inhibit_evaluation_warnings
5082 += (ifexp == truthvalue_true_node);
5083 op2 = c_fully_fold (op2, require_constant_value,
5084 &op2_maybe_const);
5085 c_inhibit_evaluation_warnings
5086 -= (ifexp == truthvalue_true_node);
5087
5088 if (warn_sign_compare)
5089 {
5090 if ((unsigned_op2
5091 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5092 || (unsigned_op1
5093 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5094 /* OK */;
5095 else if (unsigned_op2)
5096 warning_at (op1_loc, OPT_Wsign_compare,
5097 "operand of ?: changes signedness from "
5098 "%qT to %qT due to unsignedness of other "
5099 "operand", TREE_TYPE (orig_op1),
5100 TREE_TYPE (orig_op2));
5101 else
5102 warning_at (op2_loc, OPT_Wsign_compare,
5103 "operand of ?: changes signedness from "
5104 "%qT to %qT due to unsignedness of other "
5105 "operand", TREE_TYPE (orig_op2),
5106 TREE_TYPE (orig_op1));
5107 }
5108 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5109 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5110 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5111 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5112 }
5113 }
5114 }
5115 }
5116 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5117 {
5118 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5119 pedwarn (colon_loc, OPT_Wpedantic,
5120 "ISO C forbids conditional expr with only one void side");
5121 result_type = void_type_node;
5122 }
5123 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5124 {
5125 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5126 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5127 addr_space_t as_common;
5128
5129 if (comp_target_types (colon_loc, type1, type2))
5130 result_type = common_pointer_type (type1, type2);
5131 else if (null_pointer_constant_p (orig_op1))
5132 result_type = type2;
5133 else if (null_pointer_constant_p (orig_op2))
5134 result_type = type1;
5135 else if (!addr_space_superset (as1, as2, &as_common))
5136 {
5137 error_at (colon_loc, "pointers to disjoint address spaces "
5138 "used in conditional expression");
5139 return error_mark_node;
5140 }
5141 else if (VOID_TYPE_P (TREE_TYPE (type1))
5142 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5143 {
5144 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5145 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5146 & ~TYPE_QUALS (TREE_TYPE (type1))))
5147 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5148 "pointer to array loses qualifier "
5149 "in conditional expression");
5150
5151 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5152 pedwarn (colon_loc, OPT_Wpedantic,
5153 "ISO C forbids conditional expr between "
5154 "%<void *%> and function pointer");
5155 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5156 TREE_TYPE (type2)));
5157 }
5158 else if (VOID_TYPE_P (TREE_TYPE (type2))
5159 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5160 {
5161 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5162 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5163 & ~TYPE_QUALS (TREE_TYPE (type2))))
5164 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5165 "pointer to array loses qualifier "
5166 "in conditional expression");
5167
5168 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5169 pedwarn (colon_loc, OPT_Wpedantic,
5170 "ISO C forbids conditional expr between "
5171 "%<void *%> and function pointer");
5172 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5173 TREE_TYPE (type1)));
5174 }
5175 /* Objective-C pointer comparisons are a bit more lenient. */
5176 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5177 result_type = objc_common_type (type1, type2);
5178 else
5179 {
5180 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5181
5182 pedwarn (colon_loc, 0,
5183 "pointer type mismatch in conditional expression");
5184 result_type = build_pointer_type
5185 (build_qualified_type (void_type_node, qual));
5186 }
5187 }
5188 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5189 {
5190 if (!null_pointer_constant_p (orig_op2))
5191 pedwarn (colon_loc, 0,
5192 "pointer/integer type mismatch in conditional expression");
5193 else
5194 {
5195 op2 = null_pointer_node;
5196 }
5197 result_type = type1;
5198 }
5199 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5200 {
5201 if (!null_pointer_constant_p (orig_op1))
5202 pedwarn (colon_loc, 0,
5203 "pointer/integer type mismatch in conditional expression");
5204 else
5205 {
5206 op1 = null_pointer_node;
5207 }
5208 result_type = type2;
5209 }
5210
5211 if (!result_type)
5212 {
5213 if (flag_cond_mismatch)
5214 result_type = void_type_node;
5215 else
5216 {
5217 error_at (colon_loc, "type mismatch in conditional expression");
5218 return error_mark_node;
5219 }
5220 }
5221
5222 /* Merge const and volatile flags of the incoming types. */
5223 result_type
5224 = build_type_variant (result_type,
5225 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5226 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5227
5228 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5229 semantic_result_type);
5230 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5231 semantic_result_type);
5232
5233 if (ifexp_bcp && ifexp == truthvalue_true_node)
5234 {
5235 op2_int_operands = true;
5236 op1 = c_fully_fold (op1, require_constant_value, NULL);
5237 }
5238 if (ifexp_bcp && ifexp == truthvalue_false_node)
5239 {
5240 op1_int_operands = true;
5241 op2 = c_fully_fold (op2, require_constant_value, NULL);
5242 }
5243 int_const = int_operands = (ifexp_int_operands
5244 && op1_int_operands
5245 && op2_int_operands);
5246 if (int_operands)
5247 {
5248 int_const = ((ifexp == truthvalue_true_node
5249 && TREE_CODE (orig_op1) == INTEGER_CST
5250 && !TREE_OVERFLOW (orig_op1))
5251 || (ifexp == truthvalue_false_node
5252 && TREE_CODE (orig_op2) == INTEGER_CST
5253 && !TREE_OVERFLOW (orig_op2)));
5254 }
5255
5256 /* Need to convert condition operand into a vector mask. */
5257 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5258 {
5259 tree vectype = TREE_TYPE (ifexp);
5260 tree elem_type = TREE_TYPE (vectype);
5261 tree zero = build_int_cst (elem_type, 0);
5262 tree zero_vec = build_vector_from_val (vectype, zero);
5263 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5264 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5265 }
5266
5267 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5268 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5269 else
5270 {
5271 if (int_operands)
5272 {
5273 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5274 nested inside of the expression. */
5275 op1 = c_fully_fold (op1, false, NULL);
5276 op2 = c_fully_fold (op2, false, NULL);
5277 }
5278 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5279 if (int_operands)
5280 ret = note_integer_operands (ret);
5281 }
5282 if (semantic_result_type)
5283 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5284
5285 protected_set_expr_location (ret, colon_loc);
5286
5287 /* If the OP1 and OP2 are the same and don't have side-effects,
5288 warn here, because the COND_EXPR will be turned into OP1. */
5289 if (warn_duplicated_branches
5290 && TREE_CODE (ret) == COND_EXPR
5291 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5292 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5293 "this condition has identical branches");
5294
5295 return ret;
5296 }
5297
5298 /* Return a compound expression that performs two expressions and
5299 returns the value of the second of them.
5300
5301 LOC is the location of the COMPOUND_EXPR. */
5302
5303 tree
build_compound_expr(location_t loc,tree expr1,tree expr2)5304 build_compound_expr (location_t loc, tree expr1, tree expr2)
5305 {
5306 bool expr1_int_operands, expr2_int_operands;
5307 tree eptype = NULL_TREE;
5308 tree ret;
5309
5310 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5311 if (expr1_int_operands)
5312 expr1 = remove_c_maybe_const_expr (expr1);
5313 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5314 if (expr2_int_operands)
5315 expr2 = remove_c_maybe_const_expr (expr2);
5316
5317 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5318 expr1 = TREE_OPERAND (expr1, 0);
5319 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5320 {
5321 eptype = TREE_TYPE (expr2);
5322 expr2 = TREE_OPERAND (expr2, 0);
5323 }
5324
5325 if (!TREE_SIDE_EFFECTS (expr1))
5326 {
5327 /* The left-hand operand of a comma expression is like an expression
5328 statement: with -Wunused, we should warn if it doesn't have
5329 any side-effects, unless it was explicitly cast to (void). */
5330 if (warn_unused_value)
5331 {
5332 if (VOID_TYPE_P (TREE_TYPE (expr1))
5333 && CONVERT_EXPR_P (expr1))
5334 ; /* (void) a, b */
5335 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5336 && TREE_CODE (expr1) == COMPOUND_EXPR
5337 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5338 ; /* (void) a, (void) b, c */
5339 else
5340 warning_at (loc, OPT_Wunused_value,
5341 "left-hand operand of comma expression has no effect");
5342 }
5343 }
5344 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5345 && warn_unused_value)
5346 {
5347 tree r = expr1;
5348 location_t cloc = loc;
5349 while (TREE_CODE (r) == COMPOUND_EXPR)
5350 {
5351 if (EXPR_HAS_LOCATION (r))
5352 cloc = EXPR_LOCATION (r);
5353 r = TREE_OPERAND (r, 1);
5354 }
5355 if (!TREE_SIDE_EFFECTS (r)
5356 && !VOID_TYPE_P (TREE_TYPE (r))
5357 && !CONVERT_EXPR_P (r))
5358 warning_at (cloc, OPT_Wunused_value,
5359 "right-hand operand of comma expression has no effect");
5360 }
5361
5362 /* With -Wunused, we should also warn if the left-hand operand does have
5363 side-effects, but computes a value which is not used. For example, in
5364 `foo() + bar(), baz()' the result of the `+' operator is not used,
5365 so we should issue a warning. */
5366 else if (warn_unused_value)
5367 warn_if_unused_value (expr1, loc);
5368
5369 if (expr2 == error_mark_node)
5370 return error_mark_node;
5371
5372 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5373
5374 if (flag_isoc99
5375 && expr1_int_operands
5376 && expr2_int_operands)
5377 ret = note_integer_operands (ret);
5378
5379 if (eptype)
5380 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5381
5382 protected_set_expr_location (ret, loc);
5383 return ret;
5384 }
5385
5386 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5387 which we are casting. OTYPE is the type of the expression being
5388 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5389 of the cast. -Wcast-qual appeared on the command line. Named
5390 address space qualifiers are not handled here, because they result
5391 in different warnings. */
5392
5393 static void
handle_warn_cast_qual(location_t loc,tree type,tree otype)5394 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5395 {
5396 tree in_type = type;
5397 tree in_otype = otype;
5398 int added = 0;
5399 int discarded = 0;
5400 bool is_const;
5401
5402 /* Check that the qualifiers on IN_TYPE are a superset of the
5403 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5404 nodes is uninteresting and we stop as soon as we hit a
5405 non-POINTER_TYPE node on either type. */
5406 do
5407 {
5408 in_otype = TREE_TYPE (in_otype);
5409 in_type = TREE_TYPE (in_type);
5410
5411 /* GNU C allows cv-qualified function types. 'const' means the
5412 function is very pure, 'volatile' means it can't return. We
5413 need to warn when such qualifiers are added, not when they're
5414 taken away. */
5415 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5416 && TREE_CODE (in_type) == FUNCTION_TYPE)
5417 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5418 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5419 else
5420 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5421 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5422 }
5423 while (TREE_CODE (in_type) == POINTER_TYPE
5424 && TREE_CODE (in_otype) == POINTER_TYPE);
5425
5426 if (added)
5427 warning_at (loc, OPT_Wcast_qual,
5428 "cast adds %q#v qualifier to function type", added);
5429
5430 if (discarded)
5431 /* There are qualifiers present in IN_OTYPE that are not present
5432 in IN_TYPE. */
5433 warning_at (loc, OPT_Wcast_qual,
5434 "cast discards %qv qualifier from pointer target type",
5435 discarded);
5436
5437 if (added || discarded)
5438 return;
5439
5440 /* A cast from **T to const **T is unsafe, because it can cause a
5441 const value to be changed with no additional warning. We only
5442 issue this warning if T is the same on both sides, and we only
5443 issue the warning if there are the same number of pointers on
5444 both sides, as otherwise the cast is clearly unsafe anyhow. A
5445 cast is unsafe when a qualifier is added at one level and const
5446 is not present at all outer levels.
5447
5448 To issue this warning, we check at each level whether the cast
5449 adds new qualifiers not already seen. We don't need to special
5450 case function types, as they won't have the same
5451 TYPE_MAIN_VARIANT. */
5452
5453 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5454 return;
5455 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5456 return;
5457
5458 in_type = type;
5459 in_otype = otype;
5460 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5461 do
5462 {
5463 in_type = TREE_TYPE (in_type);
5464 in_otype = TREE_TYPE (in_otype);
5465 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5466 && !is_const)
5467 {
5468 warning_at (loc, OPT_Wcast_qual,
5469 "to be safe all intermediate pointers in cast from "
5470 "%qT to %qT must be %<const%> qualified",
5471 otype, type);
5472 break;
5473 }
5474 if (is_const)
5475 is_const = TYPE_READONLY (in_type);
5476 }
5477 while (TREE_CODE (in_type) == POINTER_TYPE);
5478 }
5479
5480 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5481
5482 static bool
c_safe_arg_type_equiv_p(tree t1,tree t2)5483 c_safe_arg_type_equiv_p (tree t1, tree t2)
5484 {
5485 t1 = TYPE_MAIN_VARIANT (t1);
5486 t2 = TYPE_MAIN_VARIANT (t2);
5487
5488 if (TREE_CODE (t1) == POINTER_TYPE
5489 && TREE_CODE (t2) == POINTER_TYPE)
5490 return true;
5491
5492 /* The signedness of the parameter matters only when an integral
5493 type smaller than int is promoted to int, otherwise only the
5494 precision of the parameter matters.
5495 This check should make sure that the callee does not see
5496 undefined values in argument registers. */
5497 if (INTEGRAL_TYPE_P (t1)
5498 && INTEGRAL_TYPE_P (t2)
5499 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5500 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5501 || !targetm.calls.promote_prototypes (NULL_TREE)
5502 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5503 return true;
5504
5505 return comptypes (t1, t2);
5506 }
5507
5508 /* Check if a type cast between two function types can be considered safe. */
5509
5510 static bool
c_safe_function_type_cast_p(tree t1,tree t2)5511 c_safe_function_type_cast_p (tree t1, tree t2)
5512 {
5513 if (TREE_TYPE (t1) == void_type_node &&
5514 TYPE_ARG_TYPES (t1) == void_list_node)
5515 return true;
5516
5517 if (TREE_TYPE (t2) == void_type_node &&
5518 TYPE_ARG_TYPES (t2) == void_list_node)
5519 return true;
5520
5521 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5522 return false;
5523
5524 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5525 t1 && t2;
5526 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5527 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5528 return false;
5529
5530 return true;
5531 }
5532
5533 /* Build an expression representing a cast to type TYPE of expression EXPR.
5534 LOC is the location of the cast-- typically the open paren of the cast. */
5535
5536 tree
build_c_cast(location_t loc,tree type,tree expr)5537 build_c_cast (location_t loc, tree type, tree expr)
5538 {
5539 tree value;
5540
5541 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5542 expr = TREE_OPERAND (expr, 0);
5543
5544 value = expr;
5545
5546 if (type == error_mark_node || expr == error_mark_node)
5547 return error_mark_node;
5548
5549 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5550 only in <protocol> qualifications. But when constructing cast expressions,
5551 the protocols do matter and must be kept around. */
5552 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5553 return build1 (NOP_EXPR, type, expr);
5554
5555 type = TYPE_MAIN_VARIANT (type);
5556
5557 if (TREE_CODE (type) == ARRAY_TYPE)
5558 {
5559 error_at (loc, "cast specifies array type");
5560 return error_mark_node;
5561 }
5562
5563 if (TREE_CODE (type) == FUNCTION_TYPE)
5564 {
5565 error_at (loc, "cast specifies function type");
5566 return error_mark_node;
5567 }
5568
5569 if (!VOID_TYPE_P (type))
5570 {
5571 value = require_complete_type (loc, value);
5572 if (value == error_mark_node)
5573 return error_mark_node;
5574 }
5575
5576 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5577 {
5578 if (RECORD_OR_UNION_TYPE_P (type))
5579 pedwarn (loc, OPT_Wpedantic,
5580 "ISO C forbids casting nonscalar to the same type");
5581
5582 /* Convert to remove any qualifiers from VALUE's type. */
5583 value = convert (type, value);
5584 }
5585 else if (TREE_CODE (type) == UNION_TYPE)
5586 {
5587 tree field;
5588
5589 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5590 if (TREE_TYPE (field) != error_mark_node
5591 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5592 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5593 break;
5594
5595 if (field)
5596 {
5597 tree t;
5598 bool maybe_const = true;
5599
5600 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5601 t = c_fully_fold (value, false, &maybe_const);
5602 t = build_constructor_single (type, field, t);
5603 if (!maybe_const)
5604 t = c_wrap_maybe_const (t, true);
5605 t = digest_init (loc, type, t,
5606 NULL_TREE, false, true, 0);
5607 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5608 return t;
5609 }
5610 error_at (loc, "cast to union type from type not present in union");
5611 return error_mark_node;
5612 }
5613 else
5614 {
5615 tree otype, ovalue;
5616
5617 if (type == void_type_node)
5618 {
5619 tree t = build1 (CONVERT_EXPR, type, value);
5620 SET_EXPR_LOCATION (t, loc);
5621 return t;
5622 }
5623
5624 otype = TREE_TYPE (value);
5625
5626 /* Optionally warn about potentially worrisome casts. */
5627 if (warn_cast_qual
5628 && TREE_CODE (type) == POINTER_TYPE
5629 && TREE_CODE (otype) == POINTER_TYPE)
5630 handle_warn_cast_qual (loc, type, otype);
5631
5632 /* Warn about conversions between pointers to disjoint
5633 address spaces. */
5634 if (TREE_CODE (type) == POINTER_TYPE
5635 && TREE_CODE (otype) == POINTER_TYPE
5636 && !null_pointer_constant_p (value))
5637 {
5638 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5639 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5640 addr_space_t as_common;
5641
5642 if (!addr_space_superset (as_to, as_from, &as_common))
5643 {
5644 if (ADDR_SPACE_GENERIC_P (as_from))
5645 warning_at (loc, 0, "cast to %s address space pointer "
5646 "from disjoint generic address space pointer",
5647 c_addr_space_name (as_to));
5648
5649 else if (ADDR_SPACE_GENERIC_P (as_to))
5650 warning_at (loc, 0, "cast to generic address space pointer "
5651 "from disjoint %s address space pointer",
5652 c_addr_space_name (as_from));
5653
5654 else
5655 warning_at (loc, 0, "cast to %s address space pointer "
5656 "from disjoint %s address space pointer",
5657 c_addr_space_name (as_to),
5658 c_addr_space_name (as_from));
5659 }
5660 }
5661
5662 /* Warn about possible alignment problems. */
5663 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
5664 && TREE_CODE (type) == POINTER_TYPE
5665 && TREE_CODE (otype) == POINTER_TYPE
5666 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5667 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5668 /* Don't warn about opaque types, where the actual alignment
5669 restriction is unknown. */
5670 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5671 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5672 && min_align_of_type (TREE_TYPE (type))
5673 > min_align_of_type (TREE_TYPE (otype)))
5674 warning_at (loc, OPT_Wcast_align,
5675 "cast increases required alignment of target type");
5676
5677 if (TREE_CODE (type) == INTEGER_TYPE
5678 && TREE_CODE (otype) == POINTER_TYPE
5679 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5680 /* Unlike conversion of integers to pointers, where the
5681 warning is disabled for converting constants because
5682 of cases such as SIG_*, warn about converting constant
5683 pointers to integers. In some cases it may cause unwanted
5684 sign extension, and a warning is appropriate. */
5685 warning_at (loc, OPT_Wpointer_to_int_cast,
5686 "cast from pointer to integer of different size");
5687
5688 if (TREE_CODE (value) == CALL_EXPR
5689 && TREE_CODE (type) != TREE_CODE (otype))
5690 warning_at (loc, OPT_Wbad_function_cast,
5691 "cast from function call of type %qT "
5692 "to non-matching type %qT", otype, type);
5693
5694 if (TREE_CODE (type) == POINTER_TYPE
5695 && TREE_CODE (otype) == INTEGER_TYPE
5696 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5697 /* Don't warn about converting any constant. */
5698 && !TREE_CONSTANT (value))
5699 warning_at (loc,
5700 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5701 "of different size");
5702
5703 if (warn_strict_aliasing <= 2)
5704 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
5705
5706 /* If pedantic, warn for conversions between function and object
5707 pointer types, except for converting a null pointer constant
5708 to function pointer type. */
5709 if (pedantic
5710 && TREE_CODE (type) == POINTER_TYPE
5711 && TREE_CODE (otype) == POINTER_TYPE
5712 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5713 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5714 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5715 "conversion of function pointer to object pointer type");
5716
5717 if (pedantic
5718 && TREE_CODE (type) == POINTER_TYPE
5719 && TREE_CODE (otype) == POINTER_TYPE
5720 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5721 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5722 && !null_pointer_constant_p (value))
5723 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5724 "conversion of object pointer to function pointer type");
5725
5726 if (TREE_CODE (type) == POINTER_TYPE
5727 && TREE_CODE (otype) == POINTER_TYPE
5728 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5729 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5730 && !c_safe_function_type_cast_p (TREE_TYPE (type),
5731 TREE_TYPE (otype)))
5732 warning_at (loc, OPT_Wcast_function_type,
5733 "cast between incompatible function types"
5734 " from %qT to %qT", otype, type);
5735
5736 ovalue = value;
5737 value = convert (type, value);
5738
5739 /* Ignore any integer overflow caused by the cast. */
5740 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5741 {
5742 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5743 {
5744 if (!TREE_OVERFLOW (value))
5745 {
5746 /* Avoid clobbering a shared constant. */
5747 value = copy_node (value);
5748 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5749 }
5750 }
5751 else if (TREE_OVERFLOW (value))
5752 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5753 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
5754 }
5755 }
5756
5757 /* Don't let a cast be an lvalue. */
5758 if (lvalue_p (value))
5759 value = non_lvalue_loc (loc, value);
5760
5761 /* Don't allow the results of casting to floating-point or complex
5762 types be confused with actual constants, or casts involving
5763 integer and pointer types other than direct integer-to-integer
5764 and integer-to-pointer be confused with integer constant
5765 expressions and null pointer constants. */
5766 if (TREE_CODE (value) == REAL_CST
5767 || TREE_CODE (value) == COMPLEX_CST
5768 || (TREE_CODE (value) == INTEGER_CST
5769 && !((TREE_CODE (expr) == INTEGER_CST
5770 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5771 || TREE_CODE (expr) == REAL_CST
5772 || TREE_CODE (expr) == COMPLEX_CST)))
5773 value = build1 (NOP_EXPR, type, value);
5774
5775 protected_set_expr_location (value, loc);
5776 return value;
5777 }
5778
5779 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5780 location of the open paren of the cast, or the position of the cast
5781 expr. */
5782 tree
c_cast_expr(location_t loc,struct c_type_name * type_name,tree expr)5783 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5784 {
5785 tree type;
5786 tree type_expr = NULL_TREE;
5787 bool type_expr_const = true;
5788 tree ret;
5789 int saved_wsp = warn_strict_prototypes;
5790
5791 /* This avoids warnings about unprototyped casts on
5792 integers. E.g. "#define SIG_DFL (void(*)())0". */
5793 if (TREE_CODE (expr) == INTEGER_CST)
5794 warn_strict_prototypes = 0;
5795 type = groktypename (type_name, &type_expr, &type_expr_const);
5796 warn_strict_prototypes = saved_wsp;
5797
5798 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5799 && reject_gcc_builtin (expr))
5800 return error_mark_node;
5801
5802 ret = build_c_cast (loc, type, expr);
5803 if (type_expr)
5804 {
5805 bool inner_expr_const = true;
5806 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5807 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5808 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5809 && inner_expr_const);
5810 SET_EXPR_LOCATION (ret, loc);
5811 }
5812
5813 if (!EXPR_HAS_LOCATION (ret))
5814 protected_set_expr_location (ret, loc);
5815
5816 /* C++ does not permits types to be defined in a cast, but it
5817 allows references to incomplete types. */
5818 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5819 warning_at (loc, OPT_Wc___compat,
5820 "defining a type in a cast is invalid in C++");
5821
5822 return ret;
5823 }
5824
5825 /* Build an assignment expression of lvalue LHS from value RHS.
5826 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5827 may differ from TREE_TYPE (LHS) for an enum bitfield.
5828 MODIFYCODE is the code for a binary operator that we use
5829 to combine the old value of LHS with RHS to get the new value.
5830 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5831 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5832 which may differ from TREE_TYPE (RHS) for an enum value.
5833
5834 LOCATION is the location of the MODIFYCODE operator.
5835 RHS_LOC is the location of the RHS. */
5836
5837 tree
build_modify_expr(location_t location,tree lhs,tree lhs_origtype,enum tree_code modifycode,location_t rhs_loc,tree rhs,tree rhs_origtype)5838 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5839 enum tree_code modifycode,
5840 location_t rhs_loc, tree rhs, tree rhs_origtype)
5841 {
5842 tree result;
5843 tree newrhs;
5844 tree rhseval = NULL_TREE;
5845 tree lhstype = TREE_TYPE (lhs);
5846 tree olhstype = lhstype;
5847 bool npc;
5848 bool is_atomic_op;
5849
5850 /* Types that aren't fully specified cannot be used in assignments. */
5851 lhs = require_complete_type (location, lhs);
5852
5853 /* Avoid duplicate error messages from operands that had errors. */
5854 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5855 return error_mark_node;
5856
5857 /* Ensure an error for assigning a non-lvalue array to an array in
5858 C90. */
5859 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5860 {
5861 error_at (location, "assignment to expression with array type");
5862 return error_mark_node;
5863 }
5864
5865 /* For ObjC properties, defer this check. */
5866 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5867 return error_mark_node;
5868
5869 is_atomic_op = really_atomic_lvalue (lhs);
5870
5871 newrhs = rhs;
5872
5873 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5874 {
5875 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5876 lhs_origtype, modifycode, rhs_loc, rhs,
5877 rhs_origtype);
5878 if (inner == error_mark_node)
5879 return error_mark_node;
5880 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5881 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5882 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5883 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5884 protected_set_expr_location (result, location);
5885 return result;
5886 }
5887
5888 /* If a binary op has been requested, combine the old LHS value with the RHS
5889 producing the value we should actually store into the LHS. */
5890
5891 if (modifycode != NOP_EXPR)
5892 {
5893 lhs = c_fully_fold (lhs, false, NULL, true);
5894 lhs = stabilize_reference (lhs);
5895
5896 /* Construct the RHS for any non-atomic compound assignemnt. */
5897 if (!is_atomic_op)
5898 {
5899 /* If in LHS op= RHS the RHS has side-effects, ensure they
5900 are preevaluated before the rest of the assignment expression's
5901 side-effects, because RHS could contain e.g. function calls
5902 that modify LHS. */
5903 if (TREE_SIDE_EFFECTS (rhs))
5904 {
5905 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5906 newrhs = save_expr (TREE_OPERAND (rhs, 0));
5907 else
5908 newrhs = save_expr (rhs);
5909 rhseval = newrhs;
5910 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5911 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
5912 newrhs);
5913 }
5914 newrhs = build_binary_op (location,
5915 modifycode, lhs, newrhs, true);
5916
5917 /* The original type of the right hand side is no longer
5918 meaningful. */
5919 rhs_origtype = NULL_TREE;
5920 }
5921 }
5922
5923 if (c_dialect_objc ())
5924 {
5925 /* Check if we are modifying an Objective-C property reference;
5926 if so, we need to generate setter calls. */
5927 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
5928 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
5929 else
5930 result = objc_maybe_build_modify_expr (lhs, newrhs);
5931 if (result)
5932 goto return_result;
5933
5934 /* Else, do the check that we postponed for Objective-C. */
5935 if (!lvalue_or_else (location, lhs, lv_assign))
5936 return error_mark_node;
5937 }
5938
5939 /* Give an error for storing in something that is 'const'. */
5940
5941 if (TYPE_READONLY (lhstype)
5942 || (RECORD_OR_UNION_TYPE_P (lhstype)
5943 && C_TYPE_FIELDS_READONLY (lhstype)))
5944 {
5945 readonly_error (location, lhs, lv_assign);
5946 return error_mark_node;
5947 }
5948 else if (TREE_READONLY (lhs))
5949 readonly_warning (lhs, lv_assign);
5950
5951 /* If storing into a structure or union member,
5952 it has probably been given type `int'.
5953 Compute the type that would go with
5954 the actual amount of storage the member occupies. */
5955
5956 if (TREE_CODE (lhs) == COMPONENT_REF
5957 && (TREE_CODE (lhstype) == INTEGER_TYPE
5958 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5959 || TREE_CODE (lhstype) == REAL_TYPE
5960 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5961 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5962
5963 /* If storing in a field that is in actuality a short or narrower than one,
5964 we must store in the field in its actual type. */
5965
5966 if (lhstype != TREE_TYPE (lhs))
5967 {
5968 lhs = copy_node (lhs);
5969 TREE_TYPE (lhs) = lhstype;
5970 }
5971
5972 /* Issue -Wc++-compat warnings about an assignment to an enum type
5973 when LHS does not have its original type. This happens for,
5974 e.g., an enum bitfield in a struct. */
5975 if (warn_cxx_compat
5976 && lhs_origtype != NULL_TREE
5977 && lhs_origtype != lhstype
5978 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5979 {
5980 tree checktype = (rhs_origtype != NULL_TREE
5981 ? rhs_origtype
5982 : TREE_TYPE (rhs));
5983 if (checktype != error_mark_node
5984 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5985 || (is_atomic_op && modifycode != NOP_EXPR)))
5986 warning_at (location, OPT_Wc___compat,
5987 "enum conversion in assignment is invalid in C++");
5988 }
5989
5990 /* If the lhs is atomic, remove that qualifier. */
5991 if (is_atomic_op)
5992 {
5993 lhstype = build_qualified_type (lhstype,
5994 (TYPE_QUALS (lhstype)
5995 & ~TYPE_QUAL_ATOMIC));
5996 olhstype = build_qualified_type (olhstype,
5997 (TYPE_QUALS (lhstype)
5998 & ~TYPE_QUAL_ATOMIC));
5999 }
6000
6001 /* Convert new value to destination type. Fold it first, then
6002 restore any excess precision information, for the sake of
6003 conversion warnings. */
6004
6005 if (!(is_atomic_op && modifycode != NOP_EXPR))
6006 {
6007 tree rhs_semantic_type = NULL_TREE;
6008 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6009 {
6010 rhs_semantic_type = TREE_TYPE (newrhs);
6011 newrhs = TREE_OPERAND (newrhs, 0);
6012 }
6013 npc = null_pointer_constant_p (newrhs);
6014 newrhs = c_fully_fold (newrhs, false, NULL);
6015 if (rhs_semantic_type)
6016 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6017 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6018 rhs_origtype, ic_assign, npc,
6019 NULL_TREE, NULL_TREE, 0);
6020 if (TREE_CODE (newrhs) == ERROR_MARK)
6021 return error_mark_node;
6022 }
6023
6024 /* Emit ObjC write barrier, if necessary. */
6025 if (c_dialect_objc () && flag_objc_gc)
6026 {
6027 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6028 if (result)
6029 {
6030 protected_set_expr_location (result, location);
6031 goto return_result;
6032 }
6033 }
6034
6035 /* Scan operands. */
6036
6037 if (is_atomic_op)
6038 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6039 else
6040 {
6041 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6042 TREE_SIDE_EFFECTS (result) = 1;
6043 protected_set_expr_location (result, location);
6044 }
6045
6046 /* If we got the LHS in a different type for storing in,
6047 convert the result back to the nominal type of LHS
6048 so that the value we return always has the same type
6049 as the LHS argument. */
6050
6051 if (olhstype == TREE_TYPE (result))
6052 goto return_result;
6053
6054 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6055 rhs_origtype, ic_assign, false, NULL_TREE,
6056 NULL_TREE, 0);
6057 protected_set_expr_location (result, location);
6058
6059 return_result:
6060 if (rhseval)
6061 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6062 return result;
6063 }
6064
6065 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6066 This is used to implement -fplan9-extensions. */
6067
6068 static bool
find_anonymous_field_with_type(tree struct_type,tree type)6069 find_anonymous_field_with_type (tree struct_type, tree type)
6070 {
6071 tree field;
6072 bool found;
6073
6074 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6075 found = false;
6076 for (field = TYPE_FIELDS (struct_type);
6077 field != NULL_TREE;
6078 field = TREE_CHAIN (field))
6079 {
6080 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6081 ? c_build_qualified_type (TREE_TYPE (field),
6082 TYPE_QUAL_ATOMIC)
6083 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6084 if (DECL_NAME (field) == NULL
6085 && comptypes (type, fieldtype))
6086 {
6087 if (found)
6088 return false;
6089 found = true;
6090 }
6091 else if (DECL_NAME (field) == NULL
6092 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6093 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6094 {
6095 if (found)
6096 return false;
6097 found = true;
6098 }
6099 }
6100 return found;
6101 }
6102
6103 /* RHS is an expression whose type is pointer to struct. If there is
6104 an anonymous field in RHS with type TYPE, then return a pointer to
6105 that field in RHS. This is used with -fplan9-extensions. This
6106 returns NULL if no conversion could be found. */
6107
6108 static tree
convert_to_anonymous_field(location_t location,tree type,tree rhs)6109 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6110 {
6111 tree rhs_struct_type, lhs_main_type;
6112 tree field, found_field;
6113 bool found_sub_field;
6114 tree ret;
6115
6116 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6117 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6118 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6119
6120 gcc_assert (POINTER_TYPE_P (type));
6121 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6122 ? c_build_qualified_type (TREE_TYPE (type),
6123 TYPE_QUAL_ATOMIC)
6124 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6125
6126 found_field = NULL_TREE;
6127 found_sub_field = false;
6128 for (field = TYPE_FIELDS (rhs_struct_type);
6129 field != NULL_TREE;
6130 field = TREE_CHAIN (field))
6131 {
6132 if (DECL_NAME (field) != NULL_TREE
6133 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6134 continue;
6135 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6136 ? c_build_qualified_type (TREE_TYPE (field),
6137 TYPE_QUAL_ATOMIC)
6138 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6139 if (comptypes (lhs_main_type, fieldtype))
6140 {
6141 if (found_field != NULL_TREE)
6142 return NULL_TREE;
6143 found_field = field;
6144 }
6145 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6146 lhs_main_type))
6147 {
6148 if (found_field != NULL_TREE)
6149 return NULL_TREE;
6150 found_field = field;
6151 found_sub_field = true;
6152 }
6153 }
6154
6155 if (found_field == NULL_TREE)
6156 return NULL_TREE;
6157
6158 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6159 build_fold_indirect_ref (rhs), found_field,
6160 NULL_TREE);
6161 ret = build_fold_addr_expr_loc (location, ret);
6162
6163 if (found_sub_field)
6164 {
6165 ret = convert_to_anonymous_field (location, type, ret);
6166 gcc_assert (ret != NULL_TREE);
6167 }
6168
6169 return ret;
6170 }
6171
6172 /* Issue an error message for a bad initializer component.
6173 GMSGID identifies the message.
6174 The component name is taken from the spelling stack. */
6175
6176 static void
error_init(location_t loc,const char * gmsgid)6177 error_init (location_t loc, const char *gmsgid)
6178 {
6179 char *ofwhat;
6180
6181 /* The gmsgid may be a format string with %< and %>. */
6182 error_at (loc, gmsgid);
6183 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6184 if (*ofwhat)
6185 inform (loc, "(near initialization for %qs)", ofwhat);
6186 }
6187
6188 /* Issue a pedantic warning for a bad initializer component. OPT is
6189 the option OPT_* (from options.h) controlling this warning or 0 if
6190 it is unconditionally given. GMSGID identifies the message. The
6191 component name is taken from the spelling stack. */
6192
6193 static void ATTRIBUTE_GCC_DIAG (3,0)
pedwarn_init(location_t loc,int opt,const char * gmsgid,...)6194 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6195 {
6196 /* Use the location where a macro was expanded rather than where
6197 it was defined to make sure macros defined in system headers
6198 but used incorrectly elsewhere are diagnosed. */
6199 source_location exploc = expansion_point_location_if_in_system_header (loc);
6200
6201 va_list ap;
6202 va_start (ap, gmsgid);
6203 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6204 va_end (ap);
6205 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6206 if (*ofwhat && warned)
6207 inform (exploc, "(near initialization for %qs)", ofwhat);
6208 }
6209
6210 /* Issue a warning for a bad initializer component.
6211
6212 OPT is the OPT_W* value corresponding to the warning option that
6213 controls this warning. GMSGID identifies the message. The
6214 component name is taken from the spelling stack. */
6215
6216 static void
warning_init(location_t loc,int opt,const char * gmsgid)6217 warning_init (location_t loc, int opt, const char *gmsgid)
6218 {
6219 char *ofwhat;
6220 bool warned;
6221
6222 /* Use the location where a macro was expanded rather than where
6223 it was defined to make sure macros defined in system headers
6224 but used incorrectly elsewhere are diagnosed. */
6225 source_location exploc = expansion_point_location_if_in_system_header (loc);
6226
6227 /* The gmsgid may be a format string with %< and %>. */
6228 warned = warning_at (exploc, opt, gmsgid);
6229 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6230 if (*ofwhat && warned)
6231 inform (exploc, "(near initialization for %qs)", ofwhat);
6232 }
6233
6234 /* If TYPE is an array type and EXPR is a parenthesized string
6235 constant, warn if pedantic that EXPR is being used to initialize an
6236 object of type TYPE. */
6237
6238 void
maybe_warn_string_init(location_t loc,tree type,struct c_expr expr)6239 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6240 {
6241 if (pedantic
6242 && TREE_CODE (type) == ARRAY_TYPE
6243 && TREE_CODE (expr.value) == STRING_CST
6244 && expr.original_code != STRING_CST)
6245 pedwarn_init (loc, OPT_Wpedantic,
6246 "array initialized from parenthesized string constant");
6247 }
6248
6249 /* Attempt to locate the parameter with the given index within FNDECL,
6250 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6251
6252 static location_t
get_fndecl_argument_location(tree fndecl,int argnum)6253 get_fndecl_argument_location (tree fndecl, int argnum)
6254 {
6255 int i;
6256 tree param;
6257
6258 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6259 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6260 i < argnum && param;
6261 i++, param = TREE_CHAIN (param))
6262 ;
6263
6264 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6265 return DECL_SOURCE_LOCATION (FNDECL). */
6266 if (param == NULL)
6267 return DECL_SOURCE_LOCATION (fndecl);
6268
6269 return DECL_SOURCE_LOCATION (param);
6270 }
6271
6272 /* Issue a note about a mismatching argument for parameter PARMNUM
6273 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6274 Attempt to issue the note at the pertinent parameter of the decl;
6275 failing that issue it at the location of FUNDECL; failing that
6276 issue it at PLOC. */
6277
6278 static void
inform_for_arg(tree fundecl,location_t ploc,int parmnum,tree expected_type,tree actual_type)6279 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6280 tree expected_type, tree actual_type)
6281 {
6282 location_t loc;
6283 if (fundecl && !DECL_IS_BUILTIN (fundecl))
6284 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6285 else
6286 loc = ploc;
6287
6288 inform (loc,
6289 "expected %qT but argument is of type %qT",
6290 expected_type, actual_type);
6291 }
6292
6293 /* Convert value RHS to type TYPE as preparation for an assignment to
6294 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6295 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6296 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6297 constant before any folding.
6298 The real work of conversion is done by `convert'.
6299 The purpose of this function is to generate error messages
6300 for assignments that are not allowed in C.
6301 ERRTYPE says whether it is argument passing, assignment,
6302 initialization or return.
6303
6304 In the following example, '~' denotes where EXPR_LOC and '^' where
6305 LOCATION point to:
6306
6307 f (var); [ic_argpass]
6308 ^ ~~~
6309 x = var; [ic_assign]
6310 ^ ~~~;
6311 int x = var; [ic_init]
6312 ^^^
6313 return x; [ic_return]
6314 ^
6315
6316 FUNCTION is a tree for the function being called.
6317 PARMNUM is the number of the argument, for printing in error messages. */
6318
6319 static tree
convert_for_assignment(location_t location,location_t expr_loc,tree type,tree rhs,tree origtype,enum impl_conv errtype,bool null_pointer_constant,tree fundecl,tree function,int parmnum)6320 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6321 tree rhs, tree origtype, enum impl_conv errtype,
6322 bool null_pointer_constant, tree fundecl,
6323 tree function, int parmnum)
6324 {
6325 enum tree_code codel = TREE_CODE (type);
6326 tree orig_rhs = rhs;
6327 tree rhstype;
6328 enum tree_code coder;
6329 tree rname = NULL_TREE;
6330 bool objc_ok = false;
6331
6332 /* Use the expansion point location to handle cases such as user's
6333 function returning a wrong-type macro defined in a system header. */
6334 location = expansion_point_location_if_in_system_header (location);
6335
6336 if (errtype == ic_argpass)
6337 {
6338 tree selector;
6339 /* Change pointer to function to the function itself for
6340 diagnostics. */
6341 if (TREE_CODE (function) == ADDR_EXPR
6342 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6343 function = TREE_OPERAND (function, 0);
6344
6345 /* Handle an ObjC selector specially for diagnostics. */
6346 selector = objc_message_selector ();
6347 rname = function;
6348 if (selector && parmnum > 2)
6349 {
6350 rname = selector;
6351 parmnum -= 2;
6352 }
6353 }
6354
6355 /* This macro is used to emit diagnostics to ensure that all format
6356 strings are complete sentences, visible to gettext and checked at
6357 compile time. */
6358 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6359 do { \
6360 switch (errtype) \
6361 { \
6362 case ic_argpass: \
6363 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6364 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6365 break; \
6366 case ic_assign: \
6367 pedwarn (LOCATION, OPT, AS); \
6368 break; \
6369 case ic_init: \
6370 pedwarn_init (LOCATION, OPT, IN); \
6371 break; \
6372 case ic_return: \
6373 pedwarn (LOCATION, OPT, RE); \
6374 break; \
6375 default: \
6376 gcc_unreachable (); \
6377 } \
6378 } while (0)
6379
6380 /* This macro is used to emit diagnostics to ensure that all format
6381 strings are complete sentences, visible to gettext and checked at
6382 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6383 extra parameter to enumerate qualifiers. */
6384 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6385 do { \
6386 switch (errtype) \
6387 { \
6388 case ic_argpass: \
6389 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6390 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6391 break; \
6392 case ic_assign: \
6393 pedwarn (LOCATION, OPT, AS, QUALS); \
6394 break; \
6395 case ic_init: \
6396 pedwarn (LOCATION, OPT, IN, QUALS); \
6397 break; \
6398 case ic_return: \
6399 pedwarn (LOCATION, OPT, RE, QUALS); \
6400 break; \
6401 default: \
6402 gcc_unreachable (); \
6403 } \
6404 } while (0)
6405
6406 /* This macro is used to emit diagnostics to ensure that all format
6407 strings are complete sentences, visible to gettext and checked at
6408 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6409 warning_at instead of pedwarn. */
6410 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6411 do { \
6412 switch (errtype) \
6413 { \
6414 case ic_argpass: \
6415 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6416 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6417 break; \
6418 case ic_assign: \
6419 warning_at (LOCATION, OPT, AS, QUALS); \
6420 break; \
6421 case ic_init: \
6422 warning_at (LOCATION, OPT, IN, QUALS); \
6423 break; \
6424 case ic_return: \
6425 warning_at (LOCATION, OPT, RE, QUALS); \
6426 break; \
6427 default: \
6428 gcc_unreachable (); \
6429 } \
6430 } while (0)
6431
6432 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6433 rhs = TREE_OPERAND (rhs, 0);
6434
6435 rhstype = TREE_TYPE (rhs);
6436 coder = TREE_CODE (rhstype);
6437
6438 if (coder == ERROR_MARK)
6439 return error_mark_node;
6440
6441 if (c_dialect_objc ())
6442 {
6443 int parmno;
6444
6445 switch (errtype)
6446 {
6447 case ic_return:
6448 parmno = 0;
6449 break;
6450
6451 case ic_assign:
6452 parmno = -1;
6453 break;
6454
6455 case ic_init:
6456 parmno = -2;
6457 break;
6458
6459 default:
6460 parmno = parmnum;
6461 break;
6462 }
6463
6464 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6465 }
6466
6467 if (warn_cxx_compat)
6468 {
6469 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6470 if (checktype != error_mark_node
6471 && TREE_CODE (type) == ENUMERAL_TYPE
6472 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6473 switch (errtype)
6474 {
6475 case ic_argpass:
6476 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6477 "passing argument %d of %qE is invalid in C++",
6478 parmnum, rname))
6479 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6480 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6481 "expected %qT but argument is of type %qT",
6482 type, rhstype);
6483 break;
6484 case ic_assign:
6485 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6486 "%qT in assignment is invalid in C++", rhstype, type);
6487 break;
6488 case ic_init:
6489 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6490 "%qT to %qT in initialization is invalid in C++",
6491 rhstype, type);
6492 break;
6493 case ic_return:
6494 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6495 "%qT in return is invalid in C++", rhstype, type);
6496 break;
6497 default:
6498 gcc_unreachable ();
6499 }
6500 }
6501
6502 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6503 return rhs;
6504
6505 if (coder == VOID_TYPE)
6506 {
6507 /* Except for passing an argument to an unprototyped function,
6508 this is a constraint violation. When passing an argument to
6509 an unprototyped function, it is compile-time undefined;
6510 making it a constraint in that case was rejected in
6511 DR#252. */
6512 error_at (location, "void value not ignored as it ought to be");
6513 return error_mark_node;
6514 }
6515 rhs = require_complete_type (location, rhs);
6516 if (rhs == error_mark_node)
6517 return error_mark_node;
6518
6519 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6520 return error_mark_node;
6521
6522 /* A non-reference type can convert to a reference. This handles
6523 va_start, va_copy and possibly port built-ins. */
6524 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6525 {
6526 if (!lvalue_p (rhs))
6527 {
6528 error_at (location, "cannot pass rvalue to reference parameter");
6529 return error_mark_node;
6530 }
6531 if (!c_mark_addressable (rhs))
6532 return error_mark_node;
6533 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6534 SET_EXPR_LOCATION (rhs, location);
6535
6536 rhs = convert_for_assignment (location, expr_loc,
6537 build_pointer_type (TREE_TYPE (type)),
6538 rhs, origtype, errtype,
6539 null_pointer_constant, fundecl, function,
6540 parmnum);
6541 if (rhs == error_mark_node)
6542 return error_mark_node;
6543
6544 rhs = build1 (NOP_EXPR, type, rhs);
6545 SET_EXPR_LOCATION (rhs, location);
6546 return rhs;
6547 }
6548 /* Some types can interconvert without explicit casts. */
6549 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6550 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6551 return convert (type, rhs);
6552 /* Arithmetic types all interconvert, and enum is treated like int. */
6553 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6554 || codel == FIXED_POINT_TYPE
6555 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6556 || codel == BOOLEAN_TYPE)
6557 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6558 || coder == FIXED_POINT_TYPE
6559 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6560 || coder == BOOLEAN_TYPE))
6561 {
6562 tree ret;
6563 bool save = in_late_binary_op;
6564 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6565 || (coder == REAL_TYPE
6566 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6567 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6568 in_late_binary_op = true;
6569 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6570 ? expr_loc : location, type, orig_rhs);
6571 in_late_binary_op = save;
6572 return ret;
6573 }
6574
6575 /* Aggregates in different TUs might need conversion. */
6576 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6577 && codel == coder
6578 && comptypes (type, rhstype))
6579 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6580 ? expr_loc : location, type, rhs);
6581
6582 /* Conversion to a transparent union or record from its member types.
6583 This applies only to function arguments. */
6584 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6585 && TYPE_TRANSPARENT_AGGR (type))
6586 && errtype == ic_argpass)
6587 {
6588 tree memb, marginal_memb = NULL_TREE;
6589
6590 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6591 {
6592 tree memb_type = TREE_TYPE (memb);
6593
6594 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6595 TYPE_MAIN_VARIANT (rhstype)))
6596 break;
6597
6598 if (TREE_CODE (memb_type) != POINTER_TYPE)
6599 continue;
6600
6601 if (coder == POINTER_TYPE)
6602 {
6603 tree ttl = TREE_TYPE (memb_type);
6604 tree ttr = TREE_TYPE (rhstype);
6605
6606 /* Any non-function converts to a [const][volatile] void *
6607 and vice versa; otherwise, targets must be the same.
6608 Meanwhile, the lhs target must have all the qualifiers of
6609 the rhs. */
6610 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6611 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6612 || comp_target_types (location, memb_type, rhstype))
6613 {
6614 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6615 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6616 /* If this type won't generate any warnings, use it. */
6617 if (lquals == rquals
6618 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6619 && TREE_CODE (ttl) == FUNCTION_TYPE)
6620 ? ((lquals | rquals) == rquals)
6621 : ((lquals | rquals) == lquals)))
6622 break;
6623
6624 /* Keep looking for a better type, but remember this one. */
6625 if (!marginal_memb)
6626 marginal_memb = memb;
6627 }
6628 }
6629
6630 /* Can convert integer zero to any pointer type. */
6631 if (null_pointer_constant)
6632 {
6633 rhs = null_pointer_node;
6634 break;
6635 }
6636 }
6637
6638 if (memb || marginal_memb)
6639 {
6640 if (!memb)
6641 {
6642 /* We have only a marginally acceptable member type;
6643 it needs a warning. */
6644 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6645 tree ttr = TREE_TYPE (rhstype);
6646
6647 /* Const and volatile mean something different for function
6648 types, so the usual warnings are not appropriate. */
6649 if (TREE_CODE (ttr) == FUNCTION_TYPE
6650 && TREE_CODE (ttl) == FUNCTION_TYPE)
6651 {
6652 /* Because const and volatile on functions are
6653 restrictions that say the function will not do
6654 certain things, it is okay to use a const or volatile
6655 function where an ordinary one is wanted, but not
6656 vice-versa. */
6657 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6658 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6659 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6660 OPT_Wdiscarded_qualifiers,
6661 G_("passing argument %d of %qE "
6662 "makes %q#v qualified function "
6663 "pointer from unqualified"),
6664 G_("assignment makes %q#v qualified "
6665 "function pointer from "
6666 "unqualified"),
6667 G_("initialization makes %q#v qualified "
6668 "function pointer from "
6669 "unqualified"),
6670 G_("return makes %q#v qualified function "
6671 "pointer from unqualified"),
6672 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6673 }
6674 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6675 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6676 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6677 OPT_Wdiscarded_qualifiers,
6678 G_("passing argument %d of %qE discards "
6679 "%qv qualifier from pointer target type"),
6680 G_("assignment discards %qv qualifier "
6681 "from pointer target type"),
6682 G_("initialization discards %qv qualifier "
6683 "from pointer target type"),
6684 G_("return discards %qv qualifier from "
6685 "pointer target type"),
6686 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6687
6688 memb = marginal_memb;
6689 }
6690
6691 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6692 pedwarn (location, OPT_Wpedantic,
6693 "ISO C prohibits argument conversion to union type");
6694
6695 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6696 return build_constructor_single (type, memb, rhs);
6697 }
6698 }
6699
6700 /* Conversions among pointers */
6701 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6702 && (coder == codel))
6703 {
6704 tree ttl = TREE_TYPE (type);
6705 tree ttr = TREE_TYPE (rhstype);
6706 tree mvl = ttl;
6707 tree mvr = ttr;
6708 bool is_opaque_pointer;
6709 int target_cmp = 0; /* Cache comp_target_types () result. */
6710 addr_space_t asl;
6711 addr_space_t asr;
6712
6713 if (TREE_CODE (mvl) != ARRAY_TYPE)
6714 mvl = (TYPE_ATOMIC (mvl)
6715 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6716 TYPE_QUAL_ATOMIC)
6717 : TYPE_MAIN_VARIANT (mvl));
6718 if (TREE_CODE (mvr) != ARRAY_TYPE)
6719 mvr = (TYPE_ATOMIC (mvr)
6720 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6721 TYPE_QUAL_ATOMIC)
6722 : TYPE_MAIN_VARIANT (mvr));
6723 /* Opaque pointers are treated like void pointers. */
6724 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6725
6726 /* The Plan 9 compiler permits a pointer to a struct to be
6727 automatically converted into a pointer to an anonymous field
6728 within the struct. */
6729 if (flag_plan9_extensions
6730 && RECORD_OR_UNION_TYPE_P (mvl)
6731 && RECORD_OR_UNION_TYPE_P (mvr)
6732 && mvl != mvr)
6733 {
6734 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6735 if (new_rhs != NULL_TREE)
6736 {
6737 rhs = new_rhs;
6738 rhstype = TREE_TYPE (rhs);
6739 coder = TREE_CODE (rhstype);
6740 ttr = TREE_TYPE (rhstype);
6741 mvr = TYPE_MAIN_VARIANT (ttr);
6742 }
6743 }
6744
6745 /* C++ does not allow the implicit conversion void* -> T*. However,
6746 for the purpose of reducing the number of false positives, we
6747 tolerate the special case of
6748
6749 int *p = NULL;
6750
6751 where NULL is typically defined in C to be '(void *) 0'. */
6752 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6753 warning_at (errtype == ic_argpass ? expr_loc : location,
6754 OPT_Wc___compat,
6755 "request for implicit conversion "
6756 "from %qT to %qT not permitted in C++", rhstype, type);
6757
6758 /* See if the pointers point to incompatible address spaces. */
6759 asl = TYPE_ADDR_SPACE (ttl);
6760 asr = TYPE_ADDR_SPACE (ttr);
6761 if (!null_pointer_constant_p (rhs)
6762 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6763 {
6764 switch (errtype)
6765 {
6766 case ic_argpass:
6767 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6768 "non-enclosed address space", parmnum, rname);
6769 break;
6770 case ic_assign:
6771 error_at (location, "assignment from pointer to "
6772 "non-enclosed address space");
6773 break;
6774 case ic_init:
6775 error_at (location, "initialization from pointer to "
6776 "non-enclosed address space");
6777 break;
6778 case ic_return:
6779 error_at (location, "return from pointer to "
6780 "non-enclosed address space");
6781 break;
6782 default:
6783 gcc_unreachable ();
6784 }
6785 return error_mark_node;
6786 }
6787
6788 /* Check if the right-hand side has a format attribute but the
6789 left-hand side doesn't. */
6790 if (warn_suggest_attribute_format
6791 && check_missing_format_attribute (type, rhstype))
6792 {
6793 switch (errtype)
6794 {
6795 case ic_argpass:
6796 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6797 "argument %d of %qE might be "
6798 "a candidate for a format attribute",
6799 parmnum, rname);
6800 break;
6801 case ic_assign:
6802 warning_at (location, OPT_Wsuggest_attribute_format,
6803 "assignment left-hand side might be "
6804 "a candidate for a format attribute");
6805 break;
6806 case ic_init:
6807 warning_at (location, OPT_Wsuggest_attribute_format,
6808 "initialization left-hand side might be "
6809 "a candidate for a format attribute");
6810 break;
6811 case ic_return:
6812 warning_at (location, OPT_Wsuggest_attribute_format,
6813 "return type might be "
6814 "a candidate for a format attribute");
6815 break;
6816 default:
6817 gcc_unreachable ();
6818 }
6819 }
6820
6821 /* Any non-function converts to a [const][volatile] void *
6822 and vice versa; otherwise, targets must be the same.
6823 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6824 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6825 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6826 || (target_cmp = comp_target_types (location, type, rhstype))
6827 || is_opaque_pointer
6828 || ((c_common_unsigned_type (mvl)
6829 == c_common_unsigned_type (mvr))
6830 && (c_common_signed_type (mvl)
6831 == c_common_signed_type (mvr))
6832 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6833 {
6834 /* Warn about loss of qualifers from pointers to arrays with
6835 qualifiers on the element type. */
6836 if (TREE_CODE (ttr) == ARRAY_TYPE)
6837 {
6838 ttr = strip_array_types (ttr);
6839 ttl = strip_array_types (ttl);
6840
6841 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6842 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6843 WARNING_FOR_QUALIFIERS (location, expr_loc,
6844 OPT_Wdiscarded_array_qualifiers,
6845 G_("passing argument %d of %qE discards "
6846 "%qv qualifier from pointer target type"),
6847 G_("assignment discards %qv qualifier "
6848 "from pointer target type"),
6849 G_("initialization discards %qv qualifier "
6850 "from pointer target type"),
6851 G_("return discards %qv qualifier from "
6852 "pointer target type"),
6853 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6854 }
6855 else if (pedantic
6856 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6857 ||
6858 (VOID_TYPE_P (ttr)
6859 && !null_pointer_constant
6860 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6861 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6862 G_("ISO C forbids passing argument %d of "
6863 "%qE between function pointer "
6864 "and %<void *%>"),
6865 G_("ISO C forbids assignment between "
6866 "function pointer and %<void *%>"),
6867 G_("ISO C forbids initialization between "
6868 "function pointer and %<void *%>"),
6869 G_("ISO C forbids return between function "
6870 "pointer and %<void *%>"));
6871 /* Const and volatile mean something different for function types,
6872 so the usual warnings are not appropriate. */
6873 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6874 && TREE_CODE (ttl) != FUNCTION_TYPE)
6875 {
6876 /* Don't warn about loss of qualifier for conversions from
6877 qualified void* to pointers to arrays with corresponding
6878 qualifier on the element type. */
6879 if (!pedantic)
6880 ttl = strip_array_types (ttl);
6881
6882 /* Assignments between atomic and non-atomic objects are OK. */
6883 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6884 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6885 {
6886 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6887 OPT_Wdiscarded_qualifiers,
6888 G_("passing argument %d of %qE discards "
6889 "%qv qualifier from pointer target type"),
6890 G_("assignment discards %qv qualifier "
6891 "from pointer target type"),
6892 G_("initialization discards %qv qualifier "
6893 "from pointer target type"),
6894 G_("return discards %qv qualifier from "
6895 "pointer target type"),
6896 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6897 }
6898 /* If this is not a case of ignoring a mismatch in signedness,
6899 no warning. */
6900 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6901 || target_cmp)
6902 ;
6903 /* If there is a mismatch, do warn. */
6904 else if (warn_pointer_sign)
6905 switch (errtype)
6906 {
6907 case ic_argpass:
6908 if (pedwarn (expr_loc, OPT_Wpointer_sign,
6909 "pointer targets in passing argument %d of "
6910 "%qE differ in signedness", parmnum, rname))
6911 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6912 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6913 "expected %qT but argument is of type %qT",
6914 type, rhstype);
6915 break;
6916 case ic_assign:
6917 pedwarn (location, OPT_Wpointer_sign,
6918 "pointer targets in assignment from %qT to %qT "
6919 "differ in signedness", rhstype, type);
6920 break;
6921 case ic_init:
6922 pedwarn_init (location, OPT_Wpointer_sign,
6923 "pointer targets in initialization of %qT "
6924 "from %qT differ in signedness", type,
6925 rhstype);
6926 break;
6927 case ic_return:
6928 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
6929 "returning %qT from a function with return type "
6930 "%qT differ in signedness", rhstype, type);
6931 break;
6932 default:
6933 gcc_unreachable ();
6934 }
6935 }
6936 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6937 && TREE_CODE (ttr) == FUNCTION_TYPE)
6938 {
6939 /* Because const and volatile on functions are restrictions
6940 that say the function will not do certain things,
6941 it is okay to use a const or volatile function
6942 where an ordinary one is wanted, but not vice-versa. */
6943 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6944 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6945 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6946 OPT_Wdiscarded_qualifiers,
6947 G_("passing argument %d of %qE makes "
6948 "%q#v qualified function pointer "
6949 "from unqualified"),
6950 G_("assignment makes %q#v qualified function "
6951 "pointer from unqualified"),
6952 G_("initialization makes %q#v qualified "
6953 "function pointer from unqualified"),
6954 G_("return makes %q#v qualified function "
6955 "pointer from unqualified"),
6956 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6957 }
6958 }
6959 /* Avoid warning about the volatile ObjC EH puts on decls. */
6960 else if (!objc_ok)
6961 {
6962 switch (errtype)
6963 {
6964 case ic_argpass:
6965 if (pedwarn (expr_loc, OPT_Wincompatible_pointer_types,
6966 "passing argument %d of %qE from incompatible "
6967 "pointer type", parmnum, rname))
6968 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
6969 break;
6970 case ic_assign:
6971 pedwarn (location, OPT_Wincompatible_pointer_types,
6972 "assignment to %qT from incompatible pointer type %qT",
6973 type, rhstype);
6974 break;
6975 case ic_init:
6976 pedwarn_init (location, OPT_Wincompatible_pointer_types,
6977 "initialization of %qT from incompatible pointer "
6978 "type %qT", type, rhstype);
6979 break;
6980 case ic_return:
6981 pedwarn (location, OPT_Wincompatible_pointer_types,
6982 "returning %qT from a function with incompatible "
6983 "return type %qT", rhstype, type);
6984 break;
6985 default:
6986 gcc_unreachable ();
6987 }
6988 }
6989
6990 return convert (type, rhs);
6991 }
6992 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6993 {
6994 /* ??? This should not be an error when inlining calls to
6995 unprototyped functions. */
6996 error_at (location, "invalid use of non-lvalue array");
6997 return error_mark_node;
6998 }
6999 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7000 {
7001 /* An explicit constant 0 can convert to a pointer,
7002 or one that results from arithmetic, even including
7003 a cast to integer type. */
7004 if (!null_pointer_constant)
7005 switch (errtype)
7006 {
7007 case ic_argpass:
7008 if (pedwarn (expr_loc, OPT_Wint_conversion,
7009 "passing argument %d of %qE makes pointer from "
7010 "integer without a cast", parmnum, rname))
7011 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7012 break;
7013 case ic_assign:
7014 pedwarn (location, OPT_Wint_conversion,
7015 "assignment to %qT from %qT makes pointer from integer "
7016 "without a cast", type, rhstype);
7017 break;
7018 case ic_init:
7019 pedwarn_init (location, OPT_Wint_conversion,
7020 "initialization of %qT from %qT makes pointer from "
7021 "integer without a cast", type, rhstype);
7022 break;
7023 case ic_return:
7024 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7025 "function with return type %qT makes pointer from "
7026 "integer without a cast", rhstype, type);
7027 break;
7028 default:
7029 gcc_unreachable ();
7030 }
7031
7032 return convert (type, rhs);
7033 }
7034 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7035 {
7036 switch (errtype)
7037 {
7038 case ic_argpass:
7039 if (pedwarn (expr_loc, OPT_Wint_conversion,
7040 "passing argument %d of %qE makes integer from "
7041 "pointer without a cast", parmnum, rname))
7042 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7043 break;
7044 case ic_assign:
7045 pedwarn (location, OPT_Wint_conversion,
7046 "assignment to %qT from %qT makes integer from pointer "
7047 "without a cast", type, rhstype);
7048 break;
7049 case ic_init:
7050 pedwarn_init (location, OPT_Wint_conversion,
7051 "initialization of %qT from %qT makes integer from "
7052 "pointer without a cast", type, rhstype);
7053 break;
7054 case ic_return:
7055 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7056 "function with return type %qT makes integer from "
7057 "pointer without a cast", rhstype, type);
7058 break;
7059 default:
7060 gcc_unreachable ();
7061 }
7062
7063 return convert (type, rhs);
7064 }
7065 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7066 {
7067 tree ret;
7068 bool save = in_late_binary_op;
7069 in_late_binary_op = true;
7070 ret = convert (type, rhs);
7071 in_late_binary_op = save;
7072 return ret;
7073 }
7074
7075 switch (errtype)
7076 {
7077 case ic_argpass:
7078 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
7079 rname);
7080 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7081 break;
7082 case ic_assign:
7083 error_at (location, "incompatible types when assigning to type %qT from "
7084 "type %qT", type, rhstype);
7085 break;
7086 case ic_init:
7087 error_at (location,
7088 "incompatible types when initializing type %qT using type %qT",
7089 type, rhstype);
7090 break;
7091 case ic_return:
7092 error_at (location,
7093 "incompatible types when returning type %qT but %qT was "
7094 "expected", rhstype, type);
7095 break;
7096 default:
7097 gcc_unreachable ();
7098 }
7099
7100 return error_mark_node;
7101 }
7102
7103 /* If VALUE is a compound expr all of whose expressions are constant, then
7104 return its value. Otherwise, return error_mark_node.
7105
7106 This is for handling COMPOUND_EXPRs as initializer elements
7107 which is allowed with a warning when -pedantic is specified. */
7108
7109 static tree
valid_compound_expr_initializer(tree value,tree endtype)7110 valid_compound_expr_initializer (tree value, tree endtype)
7111 {
7112 if (TREE_CODE (value) == COMPOUND_EXPR)
7113 {
7114 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7115 == error_mark_node)
7116 return error_mark_node;
7117 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7118 endtype);
7119 }
7120 else if (!initializer_constant_valid_p (value, endtype))
7121 return error_mark_node;
7122 else
7123 return value;
7124 }
7125
7126 /* Perform appropriate conversions on the initial value of a variable,
7127 store it in the declaration DECL,
7128 and print any error messages that are appropriate.
7129 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7130 If the init is invalid, store an ERROR_MARK.
7131
7132 INIT_LOC is the location of the initial value. */
7133
7134 void
store_init_value(location_t init_loc,tree decl,tree init,tree origtype)7135 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7136 {
7137 tree value, type;
7138 bool npc = false;
7139
7140 /* If variable's type was invalidly declared, just ignore it. */
7141
7142 type = TREE_TYPE (decl);
7143 if (TREE_CODE (type) == ERROR_MARK)
7144 return;
7145
7146 /* Digest the specified initializer into an expression. */
7147
7148 if (init)
7149 npc = null_pointer_constant_p (init);
7150 value = digest_init (init_loc, type, init, origtype, npc,
7151 true, TREE_STATIC (decl));
7152
7153 /* Store the expression if valid; else report error. */
7154
7155 if (!in_system_header_at (input_location)
7156 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7157 warning (OPT_Wtraditional, "traditional C rejects automatic "
7158 "aggregate initialization");
7159
7160 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7161 DECL_INITIAL (decl) = value;
7162
7163 /* ANSI wants warnings about out-of-range constant initializers. */
7164 STRIP_TYPE_NOPS (value);
7165 if (TREE_STATIC (decl))
7166 constant_expression_warning (value);
7167
7168 /* Check if we need to set array size from compound literal size. */
7169 if (TREE_CODE (type) == ARRAY_TYPE
7170 && TYPE_DOMAIN (type) == NULL_TREE
7171 && value != error_mark_node)
7172 {
7173 tree inside_init = init;
7174
7175 STRIP_TYPE_NOPS (inside_init);
7176 inside_init = fold (inside_init);
7177
7178 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7179 {
7180 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7181
7182 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7183 {
7184 /* For int foo[] = (int [3]){1}; we need to set array size
7185 now since later on array initializer will be just the
7186 brace enclosed list of the compound literal. */
7187 tree etype = strip_array_types (TREE_TYPE (decl));
7188 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7189 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7190 layout_type (type);
7191 layout_decl (cldecl, 0);
7192 TREE_TYPE (decl)
7193 = c_build_qualified_type (type, TYPE_QUALS (etype));
7194 }
7195 }
7196 }
7197 }
7198
7199 /* Methods for storing and printing names for error messages. */
7200
7201 /* Implement a spelling stack that allows components of a name to be pushed
7202 and popped. Each element on the stack is this structure. */
7203
7204 struct spelling
7205 {
7206 int kind;
7207 union
7208 {
7209 unsigned HOST_WIDE_INT i;
7210 const char *s;
7211 } u;
7212 };
7213
7214 #define SPELLING_STRING 1
7215 #define SPELLING_MEMBER 2
7216 #define SPELLING_BOUNDS 3
7217
7218 static struct spelling *spelling; /* Next stack element (unused). */
7219 static struct spelling *spelling_base; /* Spelling stack base. */
7220 static int spelling_size; /* Size of the spelling stack. */
7221
7222 /* Macros to save and restore the spelling stack around push_... functions.
7223 Alternative to SAVE_SPELLING_STACK. */
7224
7225 #define SPELLING_DEPTH() (spelling - spelling_base)
7226 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7227
7228 /* Push an element on the spelling stack with type KIND and assign VALUE
7229 to MEMBER. */
7230
7231 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7232 { \
7233 int depth = SPELLING_DEPTH (); \
7234 \
7235 if (depth >= spelling_size) \
7236 { \
7237 spelling_size += 10; \
7238 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7239 spelling_size); \
7240 RESTORE_SPELLING_DEPTH (depth); \
7241 } \
7242 \
7243 spelling->kind = (KIND); \
7244 spelling->MEMBER = (VALUE); \
7245 spelling++; \
7246 }
7247
7248 /* Push STRING on the stack. Printed literally. */
7249
7250 static void
push_string(const char * string)7251 push_string (const char *string)
7252 {
7253 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7254 }
7255
7256 /* Push a member name on the stack. Printed as '.' STRING. */
7257
7258 static void
push_member_name(tree decl)7259 push_member_name (tree decl)
7260 {
7261 const char *const string
7262 = (DECL_NAME (decl)
7263 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7264 : _("<anonymous>"));
7265 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7266 }
7267
7268 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7269
7270 static void
push_array_bounds(unsigned HOST_WIDE_INT bounds)7271 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7272 {
7273 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7274 }
7275
7276 /* Compute the maximum size in bytes of the printed spelling. */
7277
7278 static int
spelling_length(void)7279 spelling_length (void)
7280 {
7281 int size = 0;
7282 struct spelling *p;
7283
7284 for (p = spelling_base; p < spelling; p++)
7285 {
7286 if (p->kind == SPELLING_BOUNDS)
7287 size += 25;
7288 else
7289 size += strlen (p->u.s) + 1;
7290 }
7291
7292 return size;
7293 }
7294
7295 /* Print the spelling to BUFFER and return it. */
7296
7297 static char *
print_spelling(char * buffer)7298 print_spelling (char *buffer)
7299 {
7300 char *d = buffer;
7301 struct spelling *p;
7302
7303 for (p = spelling_base; p < spelling; p++)
7304 if (p->kind == SPELLING_BOUNDS)
7305 {
7306 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7307 d += strlen (d);
7308 }
7309 else
7310 {
7311 const char *s;
7312 if (p->kind == SPELLING_MEMBER)
7313 *d++ = '.';
7314 for (s = p->u.s; (*d = *s++); d++)
7315 ;
7316 }
7317 *d++ = '\0';
7318 return buffer;
7319 }
7320
7321 /* Digest the parser output INIT as an initializer for type TYPE.
7322 Return a C expression of type TYPE to represent the initial value.
7323
7324 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7325
7326 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7327
7328 If INIT is a string constant, STRICT_STRING is true if it is
7329 unparenthesized or we should not warn here for it being parenthesized.
7330 For other types of INIT, STRICT_STRING is not used.
7331
7332 INIT_LOC is the location of the INIT.
7333
7334 REQUIRE_CONSTANT requests an error if non-constant initializers or
7335 elements are seen. */
7336
7337 static tree
digest_init(location_t init_loc,tree type,tree init,tree origtype,bool null_pointer_constant,bool strict_string,int require_constant)7338 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7339 bool null_pointer_constant, bool strict_string,
7340 int require_constant)
7341 {
7342 enum tree_code code = TREE_CODE (type);
7343 tree inside_init = init;
7344 tree semantic_type = NULL_TREE;
7345 bool maybe_const = true;
7346
7347 if (type == error_mark_node
7348 || !init
7349 || error_operand_p (init))
7350 return error_mark_node;
7351
7352 STRIP_TYPE_NOPS (inside_init);
7353
7354 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7355 {
7356 semantic_type = TREE_TYPE (inside_init);
7357 inside_init = TREE_OPERAND (inside_init, 0);
7358 }
7359 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7360
7361 /* Initialization of an array of chars from a string constant
7362 optionally enclosed in braces. */
7363
7364 if (code == ARRAY_TYPE && inside_init
7365 && TREE_CODE (inside_init) == STRING_CST)
7366 {
7367 tree typ1
7368 = (TYPE_ATOMIC (TREE_TYPE (type))
7369 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7370 TYPE_QUAL_ATOMIC)
7371 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7372 /* Note that an array could be both an array of character type
7373 and an array of wchar_t if wchar_t is signed char or unsigned
7374 char. */
7375 bool char_array = (typ1 == char_type_node
7376 || typ1 == signed_char_type_node
7377 || typ1 == unsigned_char_type_node);
7378 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7379 bool char16_array = !!comptypes (typ1, char16_type_node);
7380 bool char32_array = !!comptypes (typ1, char32_type_node);
7381
7382 if (char_array || wchar_array || char16_array || char32_array)
7383 {
7384 struct c_expr expr;
7385 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7386 expr.value = inside_init;
7387 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7388 expr.original_type = NULL;
7389 maybe_warn_string_init (init_loc, type, expr);
7390
7391 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7392 pedwarn_init (init_loc, OPT_Wpedantic,
7393 "initialization of a flexible array member");
7394
7395 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7396 TYPE_MAIN_VARIANT (type)))
7397 return inside_init;
7398
7399 if (char_array)
7400 {
7401 if (typ2 != char_type_node)
7402 {
7403 error_init (init_loc, "char-array initialized from wide "
7404 "string");
7405 return error_mark_node;
7406 }
7407 }
7408 else
7409 {
7410 if (typ2 == char_type_node)
7411 {
7412 error_init (init_loc, "wide character array initialized "
7413 "from non-wide string");
7414 return error_mark_node;
7415 }
7416 else if (!comptypes(typ1, typ2))
7417 {
7418 error_init (init_loc, "wide character array initialized "
7419 "from incompatible wide string");
7420 return error_mark_node;
7421 }
7422 }
7423
7424 TREE_TYPE (inside_init) = type;
7425 if (TYPE_DOMAIN (type) != NULL_TREE
7426 && TYPE_SIZE (type) != NULL_TREE
7427 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7428 {
7429 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7430
7431 /* Subtract the size of a single (possibly wide) character
7432 because it's ok to ignore the terminating null char
7433 that is counted in the length of the constant. */
7434 if (compare_tree_int (TYPE_SIZE_UNIT (type),
7435 (len - (TYPE_PRECISION (typ1)
7436 / BITS_PER_UNIT))) < 0)
7437 pedwarn_init (init_loc, 0,
7438 ("initializer-string for array of chars "
7439 "is too long"));
7440 else if (warn_cxx_compat
7441 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7442 warning_at (init_loc, OPT_Wc___compat,
7443 ("initializer-string for array chars "
7444 "is too long for C++"));
7445 }
7446
7447 return inside_init;
7448 }
7449 else if (INTEGRAL_TYPE_P (typ1))
7450 {
7451 error_init (init_loc, "array of inappropriate type initialized "
7452 "from string constant");
7453 return error_mark_node;
7454 }
7455 }
7456
7457 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7458 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7459 below and handle as a constructor. */
7460 if (code == VECTOR_TYPE
7461 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7462 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7463 && TREE_CONSTANT (inside_init))
7464 {
7465 if (TREE_CODE (inside_init) == VECTOR_CST
7466 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7467 TYPE_MAIN_VARIANT (type)))
7468 return inside_init;
7469
7470 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7471 {
7472 unsigned HOST_WIDE_INT ix;
7473 tree value;
7474 bool constant_p = true;
7475
7476 /* Iterate through elements and check if all constructor
7477 elements are *_CSTs. */
7478 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7479 if (!CONSTANT_CLASS_P (value))
7480 {
7481 constant_p = false;
7482 break;
7483 }
7484
7485 if (constant_p)
7486 return build_vector_from_ctor (type,
7487 CONSTRUCTOR_ELTS (inside_init));
7488 }
7489 }
7490
7491 if (warn_sequence_point)
7492 verify_sequence_points (inside_init);
7493
7494 /* Any type can be initialized
7495 from an expression of the same type, optionally with braces. */
7496
7497 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7498 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7499 TYPE_MAIN_VARIANT (type))
7500 || (code == ARRAY_TYPE
7501 && comptypes (TREE_TYPE (inside_init), type))
7502 || (code == VECTOR_TYPE
7503 && comptypes (TREE_TYPE (inside_init), type))
7504 || (code == POINTER_TYPE
7505 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7506 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7507 TREE_TYPE (type)))))
7508 {
7509 if (code == POINTER_TYPE)
7510 {
7511 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7512 {
7513 if (TREE_CODE (inside_init) == STRING_CST
7514 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7515 inside_init = array_to_pointer_conversion
7516 (init_loc, inside_init);
7517 else
7518 {
7519 error_init (init_loc, "invalid use of non-lvalue array");
7520 return error_mark_node;
7521 }
7522 }
7523 }
7524
7525 if (code == VECTOR_TYPE)
7526 /* Although the types are compatible, we may require a
7527 conversion. */
7528 inside_init = convert (type, inside_init);
7529
7530 if (require_constant
7531 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7532 {
7533 /* As an extension, allow initializing objects with static storage
7534 duration with compound literals (which are then treated just as
7535 the brace enclosed list they contain). Also allow this for
7536 vectors, as we can only assign them with compound literals. */
7537 if (flag_isoc99 && code != VECTOR_TYPE)
7538 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7539 "is not constant");
7540 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7541 inside_init = DECL_INITIAL (decl);
7542 }
7543
7544 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7545 && TREE_CODE (inside_init) != CONSTRUCTOR)
7546 {
7547 error_init (init_loc, "array initialized from non-constant array "
7548 "expression");
7549 return error_mark_node;
7550 }
7551
7552 /* Compound expressions can only occur here if -Wpedantic or
7553 -pedantic-errors is specified. In the later case, we always want
7554 an error. In the former case, we simply want a warning. */
7555 if (require_constant && pedantic
7556 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7557 {
7558 inside_init
7559 = valid_compound_expr_initializer (inside_init,
7560 TREE_TYPE (inside_init));
7561 if (inside_init == error_mark_node)
7562 error_init (init_loc, "initializer element is not constant");
7563 else
7564 pedwarn_init (init_loc, OPT_Wpedantic,
7565 "initializer element is not constant");
7566 if (flag_pedantic_errors)
7567 inside_init = error_mark_node;
7568 }
7569 else if (require_constant
7570 && !initializer_constant_valid_p (inside_init,
7571 TREE_TYPE (inside_init)))
7572 {
7573 error_init (init_loc, "initializer element is not constant");
7574 inside_init = error_mark_node;
7575 }
7576 else if (require_constant && !maybe_const)
7577 pedwarn_init (init_loc, OPT_Wpedantic,
7578 "initializer element is not a constant expression");
7579
7580 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7581 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7582 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7583 type, inside_init, origtype,
7584 ic_init, null_pointer_constant,
7585 NULL_TREE, NULL_TREE, 0);
7586 return inside_init;
7587 }
7588
7589 /* Handle scalar types, including conversions. */
7590
7591 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7592 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7593 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7594 {
7595 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7596 && (TREE_CODE (init) == STRING_CST
7597 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7598 inside_init = init = array_to_pointer_conversion (init_loc, init);
7599 if (semantic_type)
7600 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7601 inside_init);
7602 inside_init
7603 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7604 inside_init, origtype, ic_init,
7605 null_pointer_constant, NULL_TREE, NULL_TREE,
7606 0);
7607
7608 /* Check to see if we have already given an error message. */
7609 if (inside_init == error_mark_node)
7610 ;
7611 else if (require_constant && !TREE_CONSTANT (inside_init))
7612 {
7613 error_init (init_loc, "initializer element is not constant");
7614 inside_init = error_mark_node;
7615 }
7616 else if (require_constant
7617 && !initializer_constant_valid_p (inside_init,
7618 TREE_TYPE (inside_init)))
7619 {
7620 error_init (init_loc, "initializer element is not computable at "
7621 "load time");
7622 inside_init = error_mark_node;
7623 }
7624 else if (require_constant && !maybe_const)
7625 pedwarn_init (init_loc, OPT_Wpedantic,
7626 "initializer element is not a constant expression");
7627
7628 return inside_init;
7629 }
7630
7631 /* Come here only for records and arrays. */
7632
7633 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7634 {
7635 error_init (init_loc, "variable-sized object may not be initialized");
7636 return error_mark_node;
7637 }
7638
7639 error_init (init_loc, "invalid initializer");
7640 return error_mark_node;
7641 }
7642
7643 /* Handle initializers that use braces. */
7644
7645 /* Type of object we are accumulating a constructor for.
7646 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7647 static tree constructor_type;
7648
7649 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7650 left to fill. */
7651 static tree constructor_fields;
7652
7653 /* For an ARRAY_TYPE, this is the specified index
7654 at which to store the next element we get. */
7655 static tree constructor_index;
7656
7657 /* For an ARRAY_TYPE, this is the maximum index. */
7658 static tree constructor_max_index;
7659
7660 /* For a RECORD_TYPE, this is the first field not yet written out. */
7661 static tree constructor_unfilled_fields;
7662
7663 /* For an ARRAY_TYPE, this is the index of the first element
7664 not yet written out. */
7665 static tree constructor_unfilled_index;
7666
7667 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7668 This is so we can generate gaps between fields, when appropriate. */
7669 static tree constructor_bit_index;
7670
7671 /* If we are saving up the elements rather than allocating them,
7672 this is the list of elements so far (in reverse order,
7673 most recent first). */
7674 static vec<constructor_elt, va_gc> *constructor_elements;
7675
7676 /* 1 if constructor should be incrementally stored into a constructor chain,
7677 0 if all the elements should be kept in AVL tree. */
7678 static int constructor_incremental;
7679
7680 /* 1 if so far this constructor's elements are all compile-time constants. */
7681 static int constructor_constant;
7682
7683 /* 1 if so far this constructor's elements are all valid address constants. */
7684 static int constructor_simple;
7685
7686 /* 1 if this constructor has an element that cannot be part of a
7687 constant expression. */
7688 static int constructor_nonconst;
7689
7690 /* 1 if this constructor is erroneous so far. */
7691 static int constructor_erroneous;
7692
7693 /* 1 if this constructor is the universal zero initializer { 0 }. */
7694 static int constructor_zeroinit;
7695
7696 /* Structure for managing pending initializer elements, organized as an
7697 AVL tree. */
7698
7699 struct init_node
7700 {
7701 struct init_node *left, *right;
7702 struct init_node *parent;
7703 int balance;
7704 tree purpose;
7705 tree value;
7706 tree origtype;
7707 };
7708
7709 /* Tree of pending elements at this constructor level.
7710 These are elements encountered out of order
7711 which belong at places we haven't reached yet in actually
7712 writing the output.
7713 Will never hold tree nodes across GC runs. */
7714 static struct init_node *constructor_pending_elts;
7715
7716 /* The SPELLING_DEPTH of this constructor. */
7717 static int constructor_depth;
7718
7719 /* DECL node for which an initializer is being read.
7720 0 means we are reading a constructor expression
7721 such as (struct foo) {...}. */
7722 static tree constructor_decl;
7723
7724 /* Nonzero if this is an initializer for a top-level decl. */
7725 static int constructor_top_level;
7726
7727 /* Nonzero if there were any member designators in this initializer. */
7728 static int constructor_designated;
7729
7730 /* Nesting depth of designator list. */
7731 static int designator_depth;
7732
7733 /* Nonzero if there were diagnosed errors in this designator list. */
7734 static int designator_erroneous;
7735
7736
7737 /* This stack has a level for each implicit or explicit level of
7738 structuring in the initializer, including the outermost one. It
7739 saves the values of most of the variables above. */
7740
7741 struct constructor_range_stack;
7742
7743 struct constructor_stack
7744 {
7745 struct constructor_stack *next;
7746 tree type;
7747 tree fields;
7748 tree index;
7749 tree max_index;
7750 tree unfilled_index;
7751 tree unfilled_fields;
7752 tree bit_index;
7753 vec<constructor_elt, va_gc> *elements;
7754 struct init_node *pending_elts;
7755 int offset;
7756 int depth;
7757 /* If value nonzero, this value should replace the entire
7758 constructor at this level. */
7759 struct c_expr replacement_value;
7760 struct constructor_range_stack *range_stack;
7761 char constant;
7762 char simple;
7763 char nonconst;
7764 char implicit;
7765 char erroneous;
7766 char outer;
7767 char incremental;
7768 char designated;
7769 int designator_depth;
7770 };
7771
7772 static struct constructor_stack *constructor_stack;
7773
7774 /* This stack represents designators from some range designator up to
7775 the last designator in the list. */
7776
7777 struct constructor_range_stack
7778 {
7779 struct constructor_range_stack *next, *prev;
7780 struct constructor_stack *stack;
7781 tree range_start;
7782 tree index;
7783 tree range_end;
7784 tree fields;
7785 };
7786
7787 static struct constructor_range_stack *constructor_range_stack;
7788
7789 /* This stack records separate initializers that are nested.
7790 Nested initializers can't happen in ANSI C, but GNU C allows them
7791 in cases like { ... (struct foo) { ... } ... }. */
7792
7793 struct initializer_stack
7794 {
7795 struct initializer_stack *next;
7796 tree decl;
7797 struct constructor_stack *constructor_stack;
7798 struct constructor_range_stack *constructor_range_stack;
7799 vec<constructor_elt, va_gc> *elements;
7800 struct spelling *spelling;
7801 struct spelling *spelling_base;
7802 int spelling_size;
7803 char top_level;
7804 char require_constant_value;
7805 char require_constant_elements;
7806 rich_location *missing_brace_richloc;
7807 };
7808
7809 static struct initializer_stack *initializer_stack;
7810
7811 /* Prepare to parse and output the initializer for variable DECL. */
7812
7813 void
start_init(tree decl,tree asmspec_tree ATTRIBUTE_UNUSED,int top_level,rich_location * richloc)7814 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
7815 rich_location *richloc)
7816 {
7817 const char *locus;
7818 struct initializer_stack *p = XNEW (struct initializer_stack);
7819
7820 p->decl = constructor_decl;
7821 p->require_constant_value = require_constant_value;
7822 p->require_constant_elements = require_constant_elements;
7823 p->constructor_stack = constructor_stack;
7824 p->constructor_range_stack = constructor_range_stack;
7825 p->elements = constructor_elements;
7826 p->spelling = spelling;
7827 p->spelling_base = spelling_base;
7828 p->spelling_size = spelling_size;
7829 p->top_level = constructor_top_level;
7830 p->next = initializer_stack;
7831 p->missing_brace_richloc = richloc;
7832 initializer_stack = p;
7833
7834 constructor_decl = decl;
7835 constructor_designated = 0;
7836 constructor_top_level = top_level;
7837
7838 if (decl != NULL_TREE && decl != error_mark_node)
7839 {
7840 require_constant_value = TREE_STATIC (decl);
7841 require_constant_elements
7842 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7843 /* For a scalar, you can always use any value to initialize,
7844 even within braces. */
7845 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7846 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7847 }
7848 else
7849 {
7850 require_constant_value = 0;
7851 require_constant_elements = 0;
7852 locus = _("(anonymous)");
7853 }
7854
7855 constructor_stack = 0;
7856 constructor_range_stack = 0;
7857
7858 found_missing_braces = 0;
7859
7860 spelling_base = 0;
7861 spelling_size = 0;
7862 RESTORE_SPELLING_DEPTH (0);
7863
7864 if (locus)
7865 push_string (locus);
7866 }
7867
7868 void
finish_init(void)7869 finish_init (void)
7870 {
7871 struct initializer_stack *p = initializer_stack;
7872
7873 /* Free the whole constructor stack of this initializer. */
7874 while (constructor_stack)
7875 {
7876 struct constructor_stack *q = constructor_stack;
7877 constructor_stack = q->next;
7878 free (q);
7879 }
7880
7881 gcc_assert (!constructor_range_stack);
7882
7883 /* Pop back to the data of the outer initializer (if any). */
7884 free (spelling_base);
7885
7886 constructor_decl = p->decl;
7887 require_constant_value = p->require_constant_value;
7888 require_constant_elements = p->require_constant_elements;
7889 constructor_stack = p->constructor_stack;
7890 constructor_range_stack = p->constructor_range_stack;
7891 constructor_elements = p->elements;
7892 spelling = p->spelling;
7893 spelling_base = p->spelling_base;
7894 spelling_size = p->spelling_size;
7895 constructor_top_level = p->top_level;
7896 initializer_stack = p->next;
7897 free (p);
7898 }
7899
7900 /* Call here when we see the initializer is surrounded by braces.
7901 This is instead of a call to push_init_level;
7902 it is matched by a call to pop_init_level.
7903
7904 TYPE is the type to initialize, for a constructor expression.
7905 For an initializer for a decl, TYPE is zero. */
7906
7907 void
really_start_incremental_init(tree type)7908 really_start_incremental_init (tree type)
7909 {
7910 struct constructor_stack *p = XNEW (struct constructor_stack);
7911
7912 if (type == NULL_TREE)
7913 type = TREE_TYPE (constructor_decl);
7914
7915 if (VECTOR_TYPE_P (type)
7916 && TYPE_VECTOR_OPAQUE (type))
7917 error ("opaque vector types cannot be initialized");
7918
7919 p->type = constructor_type;
7920 p->fields = constructor_fields;
7921 p->index = constructor_index;
7922 p->max_index = constructor_max_index;
7923 p->unfilled_index = constructor_unfilled_index;
7924 p->unfilled_fields = constructor_unfilled_fields;
7925 p->bit_index = constructor_bit_index;
7926 p->elements = constructor_elements;
7927 p->constant = constructor_constant;
7928 p->simple = constructor_simple;
7929 p->nonconst = constructor_nonconst;
7930 p->erroneous = constructor_erroneous;
7931 p->pending_elts = constructor_pending_elts;
7932 p->depth = constructor_depth;
7933 p->replacement_value.value = 0;
7934 p->replacement_value.original_code = ERROR_MARK;
7935 p->replacement_value.original_type = NULL;
7936 p->implicit = 0;
7937 p->range_stack = 0;
7938 p->outer = 0;
7939 p->incremental = constructor_incremental;
7940 p->designated = constructor_designated;
7941 p->designator_depth = designator_depth;
7942 p->next = 0;
7943 constructor_stack = p;
7944
7945 constructor_constant = 1;
7946 constructor_simple = 1;
7947 constructor_nonconst = 0;
7948 constructor_depth = SPELLING_DEPTH ();
7949 constructor_elements = NULL;
7950 constructor_pending_elts = 0;
7951 constructor_type = type;
7952 constructor_incremental = 1;
7953 constructor_designated = 0;
7954 constructor_zeroinit = 1;
7955 designator_depth = 0;
7956 designator_erroneous = 0;
7957
7958 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7959 {
7960 constructor_fields = TYPE_FIELDS (constructor_type);
7961 /* Skip any nameless bit fields at the beginning. */
7962 while (constructor_fields != NULL_TREE
7963 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
7964 constructor_fields = DECL_CHAIN (constructor_fields);
7965
7966 constructor_unfilled_fields = constructor_fields;
7967 constructor_bit_index = bitsize_zero_node;
7968 }
7969 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7970 {
7971 if (TYPE_DOMAIN (constructor_type))
7972 {
7973 constructor_max_index
7974 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7975
7976 /* Detect non-empty initializations of zero-length arrays. */
7977 if (constructor_max_index == NULL_TREE
7978 && TYPE_SIZE (constructor_type))
7979 constructor_max_index = integer_minus_one_node;
7980
7981 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7982 to initialize VLAs will cause a proper error; avoid tree
7983 checking errors as well by setting a safe value. */
7984 if (constructor_max_index
7985 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7986 constructor_max_index = integer_minus_one_node;
7987
7988 constructor_index
7989 = convert (bitsizetype,
7990 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7991 }
7992 else
7993 {
7994 constructor_index = bitsize_zero_node;
7995 constructor_max_index = NULL_TREE;
7996 }
7997
7998 constructor_unfilled_index = constructor_index;
7999 }
8000 else if (VECTOR_TYPE_P (constructor_type))
8001 {
8002 /* Vectors are like simple fixed-size arrays. */
8003 constructor_max_index =
8004 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8005 constructor_index = bitsize_zero_node;
8006 constructor_unfilled_index = constructor_index;
8007 }
8008 else
8009 {
8010 /* Handle the case of int x = {5}; */
8011 constructor_fields = constructor_type;
8012 constructor_unfilled_fields = constructor_type;
8013 }
8014 }
8015
8016 extern location_t last_init_list_comma;
8017
8018 /* Called when we see an open brace for a nested initializer. Finish
8019 off any pending levels with implicit braces. */
8020 void
finish_implicit_inits(location_t loc,struct obstack * braced_init_obstack)8021 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8022 {
8023 while (constructor_stack->implicit)
8024 {
8025 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8026 && constructor_fields == NULL_TREE)
8027 process_init_element (input_location,
8028 pop_init_level (loc, 1, braced_init_obstack,
8029 last_init_list_comma),
8030 true, braced_init_obstack);
8031 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8032 && constructor_max_index
8033 && tree_int_cst_lt (constructor_max_index,
8034 constructor_index))
8035 process_init_element (input_location,
8036 pop_init_level (loc, 1, braced_init_obstack,
8037 last_init_list_comma),
8038 true, braced_init_obstack);
8039 else
8040 break;
8041 }
8042 }
8043
8044 /* Push down into a subobject, for initialization.
8045 If this is for an explicit set of braces, IMPLICIT is 0.
8046 If it is because the next element belongs at a lower level,
8047 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8048
8049 void
push_init_level(location_t loc,int implicit,struct obstack * braced_init_obstack)8050 push_init_level (location_t loc, int implicit,
8051 struct obstack *braced_init_obstack)
8052 {
8053 struct constructor_stack *p;
8054 tree value = NULL_TREE;
8055
8056 /* Unless this is an explicit brace, we need to preserve previous
8057 content if any. */
8058 if (implicit)
8059 {
8060 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8061 value = find_init_member (constructor_fields, braced_init_obstack);
8062 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8063 value = find_init_member (constructor_index, braced_init_obstack);
8064 }
8065
8066 p = XNEW (struct constructor_stack);
8067 p->type = constructor_type;
8068 p->fields = constructor_fields;
8069 p->index = constructor_index;
8070 p->max_index = constructor_max_index;
8071 p->unfilled_index = constructor_unfilled_index;
8072 p->unfilled_fields = constructor_unfilled_fields;
8073 p->bit_index = constructor_bit_index;
8074 p->elements = constructor_elements;
8075 p->constant = constructor_constant;
8076 p->simple = constructor_simple;
8077 p->nonconst = constructor_nonconst;
8078 p->erroneous = constructor_erroneous;
8079 p->pending_elts = constructor_pending_elts;
8080 p->depth = constructor_depth;
8081 p->replacement_value.value = NULL_TREE;
8082 p->replacement_value.original_code = ERROR_MARK;
8083 p->replacement_value.original_type = NULL;
8084 p->implicit = implicit;
8085 p->outer = 0;
8086 p->incremental = constructor_incremental;
8087 p->designated = constructor_designated;
8088 p->designator_depth = designator_depth;
8089 p->next = constructor_stack;
8090 p->range_stack = 0;
8091 constructor_stack = p;
8092
8093 constructor_constant = 1;
8094 constructor_simple = 1;
8095 constructor_nonconst = 0;
8096 constructor_depth = SPELLING_DEPTH ();
8097 constructor_elements = NULL;
8098 constructor_incremental = 1;
8099 constructor_designated = 0;
8100 constructor_pending_elts = 0;
8101 if (!implicit)
8102 {
8103 p->range_stack = constructor_range_stack;
8104 constructor_range_stack = 0;
8105 designator_depth = 0;
8106 designator_erroneous = 0;
8107 }
8108
8109 /* Don't die if an entire brace-pair level is superfluous
8110 in the containing level. */
8111 if (constructor_type == NULL_TREE)
8112 ;
8113 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8114 {
8115 /* Don't die if there are extra init elts at the end. */
8116 if (constructor_fields == NULL_TREE)
8117 constructor_type = NULL_TREE;
8118 else
8119 {
8120 constructor_type = TREE_TYPE (constructor_fields);
8121 push_member_name (constructor_fields);
8122 constructor_depth++;
8123 }
8124 /* If upper initializer is designated, then mark this as
8125 designated too to prevent bogus warnings. */
8126 constructor_designated = p->designated;
8127 }
8128 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8129 {
8130 constructor_type = TREE_TYPE (constructor_type);
8131 push_array_bounds (tree_to_uhwi (constructor_index));
8132 constructor_depth++;
8133 }
8134
8135 if (constructor_type == NULL_TREE)
8136 {
8137 error_init (loc, "extra brace group at end of initializer");
8138 constructor_fields = NULL_TREE;
8139 constructor_unfilled_fields = NULL_TREE;
8140 return;
8141 }
8142
8143 if (value && TREE_CODE (value) == CONSTRUCTOR)
8144 {
8145 constructor_constant = TREE_CONSTANT (value);
8146 constructor_simple = TREE_STATIC (value);
8147 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8148 constructor_elements = CONSTRUCTOR_ELTS (value);
8149 if (!vec_safe_is_empty (constructor_elements)
8150 && (TREE_CODE (constructor_type) == RECORD_TYPE
8151 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8152 set_nonincremental_init (braced_init_obstack);
8153 }
8154
8155 if (implicit == 1)
8156 {
8157 found_missing_braces = 1;
8158 if (initializer_stack->missing_brace_richloc)
8159 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8160 (loc, "{");
8161 }
8162
8163 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8164 {
8165 constructor_fields = TYPE_FIELDS (constructor_type);
8166 /* Skip any nameless bit fields at the beginning. */
8167 while (constructor_fields != NULL_TREE
8168 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8169 constructor_fields = DECL_CHAIN (constructor_fields);
8170
8171 constructor_unfilled_fields = constructor_fields;
8172 constructor_bit_index = bitsize_zero_node;
8173 }
8174 else if (VECTOR_TYPE_P (constructor_type))
8175 {
8176 /* Vectors are like simple fixed-size arrays. */
8177 constructor_max_index =
8178 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8179 constructor_index = bitsize_int (0);
8180 constructor_unfilled_index = constructor_index;
8181 }
8182 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8183 {
8184 if (TYPE_DOMAIN (constructor_type))
8185 {
8186 constructor_max_index
8187 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8188
8189 /* Detect non-empty initializations of zero-length arrays. */
8190 if (constructor_max_index == NULL_TREE
8191 && TYPE_SIZE (constructor_type))
8192 constructor_max_index = integer_minus_one_node;
8193
8194 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8195 to initialize VLAs will cause a proper error; avoid tree
8196 checking errors as well by setting a safe value. */
8197 if (constructor_max_index
8198 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8199 constructor_max_index = integer_minus_one_node;
8200
8201 constructor_index
8202 = convert (bitsizetype,
8203 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8204 }
8205 else
8206 constructor_index = bitsize_zero_node;
8207
8208 constructor_unfilled_index = constructor_index;
8209 if (value && TREE_CODE (value) == STRING_CST)
8210 {
8211 /* We need to split the char/wchar array into individual
8212 characters, so that we don't have to special case it
8213 everywhere. */
8214 set_nonincremental_init_from_string (value, braced_init_obstack);
8215 }
8216 }
8217 else
8218 {
8219 if (constructor_type != error_mark_node)
8220 warning_init (input_location, 0, "braces around scalar initializer");
8221 constructor_fields = constructor_type;
8222 constructor_unfilled_fields = constructor_type;
8223 }
8224 }
8225
8226 /* At the end of an implicit or explicit brace level,
8227 finish up that level of constructor. If a single expression
8228 with redundant braces initialized that level, return the
8229 c_expr structure for that expression. Otherwise, the original_code
8230 element is set to ERROR_MARK.
8231 If we were outputting the elements as they are read, return 0 as the value
8232 from inner levels (process_init_element ignores that),
8233 but return error_mark_node as the value from the outermost level
8234 (that's what we want to put in DECL_INITIAL).
8235 Otherwise, return a CONSTRUCTOR expression as the value. */
8236
8237 struct c_expr
pop_init_level(location_t loc,int implicit,struct obstack * braced_init_obstack,location_t insert_before)8238 pop_init_level (location_t loc, int implicit,
8239 struct obstack *braced_init_obstack,
8240 location_t insert_before)
8241 {
8242 struct constructor_stack *p;
8243 struct c_expr ret;
8244 ret.value = NULL_TREE;
8245 ret.original_code = ERROR_MARK;
8246 ret.original_type = NULL;
8247
8248 if (implicit == 0)
8249 {
8250 /* When we come to an explicit close brace,
8251 pop any inner levels that didn't have explicit braces. */
8252 while (constructor_stack->implicit)
8253 process_init_element (input_location,
8254 pop_init_level (loc, 1, braced_init_obstack,
8255 insert_before),
8256 true, braced_init_obstack);
8257 gcc_assert (!constructor_range_stack);
8258 }
8259 else
8260 if (initializer_stack->missing_brace_richloc)
8261 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8262 (insert_before, "}");
8263
8264 /* Now output all pending elements. */
8265 constructor_incremental = 1;
8266 output_pending_init_elements (1, braced_init_obstack);
8267
8268 p = constructor_stack;
8269
8270 /* Error for initializing a flexible array member, or a zero-length
8271 array member in an inappropriate context. */
8272 if (constructor_type && constructor_fields
8273 && TREE_CODE (constructor_type) == ARRAY_TYPE
8274 && TYPE_DOMAIN (constructor_type)
8275 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8276 {
8277 /* Silently discard empty initializations. The parser will
8278 already have pedwarned for empty brackets. */
8279 if (integer_zerop (constructor_unfilled_index))
8280 constructor_type = NULL_TREE;
8281 else
8282 {
8283 gcc_assert (!TYPE_SIZE (constructor_type));
8284
8285 if (constructor_depth > 2)
8286 error_init (loc, "initialization of flexible array member in a nested context");
8287 else
8288 pedwarn_init (loc, OPT_Wpedantic,
8289 "initialization of a flexible array member");
8290
8291 /* We have already issued an error message for the existence
8292 of a flexible array member not at the end of the structure.
8293 Discard the initializer so that we do not die later. */
8294 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8295 constructor_type = NULL_TREE;
8296 }
8297 }
8298
8299 switch (vec_safe_length (constructor_elements))
8300 {
8301 case 0:
8302 /* Initialization with { } counts as zeroinit. */
8303 constructor_zeroinit = 1;
8304 break;
8305 case 1:
8306 /* This might be zeroinit as well. */
8307 if (integer_zerop ((*constructor_elements)[0].value))
8308 constructor_zeroinit = 1;
8309 break;
8310 default:
8311 /* If the constructor has more than one element, it can't be { 0 }. */
8312 constructor_zeroinit = 0;
8313 break;
8314 }
8315
8316 /* Warn when some structs are initialized with direct aggregation. */
8317 if (!implicit && found_missing_braces && warn_missing_braces
8318 && !constructor_zeroinit)
8319 {
8320 gcc_assert (initializer_stack->missing_brace_richloc);
8321 warning_at (initializer_stack->missing_brace_richloc,
8322 OPT_Wmissing_braces,
8323 "missing braces around initializer");
8324 }
8325
8326 /* Warn when some struct elements are implicitly initialized to zero. */
8327 if (warn_missing_field_initializers
8328 && constructor_type
8329 && TREE_CODE (constructor_type) == RECORD_TYPE
8330 && constructor_unfilled_fields)
8331 {
8332 /* Do not warn for flexible array members or zero-length arrays. */
8333 while (constructor_unfilled_fields
8334 && (!DECL_SIZE (constructor_unfilled_fields)
8335 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8336 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8337
8338 if (constructor_unfilled_fields
8339 /* Do not warn if this level of the initializer uses member
8340 designators; it is likely to be deliberate. */
8341 && !constructor_designated
8342 /* Do not warn about initializing with { 0 } or with { }. */
8343 && !constructor_zeroinit)
8344 {
8345 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8346 "missing initializer for field %qD of %qT",
8347 constructor_unfilled_fields,
8348 constructor_type))
8349 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8350 "%qD declared here", constructor_unfilled_fields);
8351 }
8352 }
8353
8354 /* Pad out the end of the structure. */
8355 if (p->replacement_value.value)
8356 /* If this closes a superfluous brace pair,
8357 just pass out the element between them. */
8358 ret = p->replacement_value;
8359 else if (constructor_type == NULL_TREE)
8360 ;
8361 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8362 && TREE_CODE (constructor_type) != ARRAY_TYPE
8363 && !VECTOR_TYPE_P (constructor_type))
8364 {
8365 /* A nonincremental scalar initializer--just return
8366 the element, after verifying there is just one. */
8367 if (vec_safe_is_empty (constructor_elements))
8368 {
8369 if (!constructor_erroneous)
8370 error_init (loc, "empty scalar initializer");
8371 ret.value = error_mark_node;
8372 }
8373 else if (vec_safe_length (constructor_elements) != 1)
8374 {
8375 error_init (loc, "extra elements in scalar initializer");
8376 ret.value = (*constructor_elements)[0].value;
8377 }
8378 else
8379 ret.value = (*constructor_elements)[0].value;
8380 }
8381 else
8382 {
8383 if (constructor_erroneous)
8384 ret.value = error_mark_node;
8385 else
8386 {
8387 ret.value = build_constructor (constructor_type,
8388 constructor_elements);
8389 if (constructor_constant)
8390 TREE_CONSTANT (ret.value) = 1;
8391 if (constructor_constant && constructor_simple)
8392 TREE_STATIC (ret.value) = 1;
8393 if (constructor_nonconst)
8394 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8395 }
8396 }
8397
8398 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8399 {
8400 if (constructor_nonconst)
8401 ret.original_code = C_MAYBE_CONST_EXPR;
8402 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8403 ret.original_code = ERROR_MARK;
8404 }
8405
8406 constructor_type = p->type;
8407 constructor_fields = p->fields;
8408 constructor_index = p->index;
8409 constructor_max_index = p->max_index;
8410 constructor_unfilled_index = p->unfilled_index;
8411 constructor_unfilled_fields = p->unfilled_fields;
8412 constructor_bit_index = p->bit_index;
8413 constructor_elements = p->elements;
8414 constructor_constant = p->constant;
8415 constructor_simple = p->simple;
8416 constructor_nonconst = p->nonconst;
8417 constructor_erroneous = p->erroneous;
8418 constructor_incremental = p->incremental;
8419 constructor_designated = p->designated;
8420 designator_depth = p->designator_depth;
8421 constructor_pending_elts = p->pending_elts;
8422 constructor_depth = p->depth;
8423 if (!p->implicit)
8424 constructor_range_stack = p->range_stack;
8425 RESTORE_SPELLING_DEPTH (constructor_depth);
8426
8427 constructor_stack = p->next;
8428 free (p);
8429
8430 if (ret.value == NULL_TREE && constructor_stack == 0)
8431 ret.value = error_mark_node;
8432 return ret;
8433 }
8434
8435 /* Common handling for both array range and field name designators.
8436 ARRAY argument is nonzero for array ranges. Returns false for success. */
8437
8438 static bool
set_designator(location_t loc,bool array,struct obstack * braced_init_obstack)8439 set_designator (location_t loc, bool array,
8440 struct obstack *braced_init_obstack)
8441 {
8442 tree subtype;
8443 enum tree_code subcode;
8444
8445 /* Don't die if an entire brace-pair level is superfluous
8446 in the containing level. */
8447 if (constructor_type == NULL_TREE)
8448 return true;
8449
8450 /* If there were errors in this designator list already, bail out
8451 silently. */
8452 if (designator_erroneous)
8453 return true;
8454
8455 if (!designator_depth)
8456 {
8457 gcc_assert (!constructor_range_stack);
8458
8459 /* Designator list starts at the level of closest explicit
8460 braces. */
8461 while (constructor_stack->implicit)
8462 process_init_element (input_location,
8463 pop_init_level (loc, 1, braced_init_obstack,
8464 last_init_list_comma),
8465 true, braced_init_obstack);
8466 constructor_designated = 1;
8467 return false;
8468 }
8469
8470 switch (TREE_CODE (constructor_type))
8471 {
8472 case RECORD_TYPE:
8473 case UNION_TYPE:
8474 subtype = TREE_TYPE (constructor_fields);
8475 if (subtype != error_mark_node)
8476 subtype = TYPE_MAIN_VARIANT (subtype);
8477 break;
8478 case ARRAY_TYPE:
8479 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8480 break;
8481 default:
8482 gcc_unreachable ();
8483 }
8484
8485 subcode = TREE_CODE (subtype);
8486 if (array && subcode != ARRAY_TYPE)
8487 {
8488 error_init (loc, "array index in non-array initializer");
8489 return true;
8490 }
8491 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8492 {
8493 error_init (loc, "field name not in record or union initializer");
8494 return true;
8495 }
8496
8497 constructor_designated = 1;
8498 finish_implicit_inits (loc, braced_init_obstack);
8499 push_init_level (loc, 2, braced_init_obstack);
8500 return false;
8501 }
8502
8503 /* If there are range designators in designator list, push a new designator
8504 to constructor_range_stack. RANGE_END is end of such stack range or
8505 NULL_TREE if there is no range designator at this level. */
8506
8507 static void
push_range_stack(tree range_end,struct obstack * braced_init_obstack)8508 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8509 {
8510 struct constructor_range_stack *p;
8511
8512 p = (struct constructor_range_stack *)
8513 obstack_alloc (braced_init_obstack,
8514 sizeof (struct constructor_range_stack));
8515 p->prev = constructor_range_stack;
8516 p->next = 0;
8517 p->fields = constructor_fields;
8518 p->range_start = constructor_index;
8519 p->index = constructor_index;
8520 p->stack = constructor_stack;
8521 p->range_end = range_end;
8522 if (constructor_range_stack)
8523 constructor_range_stack->next = p;
8524 constructor_range_stack = p;
8525 }
8526
8527 /* Within an array initializer, specify the next index to be initialized.
8528 FIRST is that index. If LAST is nonzero, then initialize a range
8529 of indices, running from FIRST through LAST. */
8530
8531 void
set_init_index(location_t loc,tree first,tree last,struct obstack * braced_init_obstack)8532 set_init_index (location_t loc, tree first, tree last,
8533 struct obstack *braced_init_obstack)
8534 {
8535 if (set_designator (loc, true, braced_init_obstack))
8536 return;
8537
8538 designator_erroneous = 1;
8539
8540 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8541 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8542 {
8543 error_init (loc, "array index in initializer not of integer type");
8544 return;
8545 }
8546
8547 if (TREE_CODE (first) != INTEGER_CST)
8548 {
8549 first = c_fully_fold (first, false, NULL);
8550 if (TREE_CODE (first) == INTEGER_CST)
8551 pedwarn_init (loc, OPT_Wpedantic,
8552 "array index in initializer is not "
8553 "an integer constant expression");
8554 }
8555
8556 if (last && TREE_CODE (last) != INTEGER_CST)
8557 {
8558 last = c_fully_fold (last, false, NULL);
8559 if (TREE_CODE (last) == INTEGER_CST)
8560 pedwarn_init (loc, OPT_Wpedantic,
8561 "array index in initializer is not "
8562 "an integer constant expression");
8563 }
8564
8565 if (TREE_CODE (first) != INTEGER_CST)
8566 error_init (loc, "nonconstant array index in initializer");
8567 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
8568 error_init (loc, "nonconstant array index in initializer");
8569 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8570 error_init (loc, "array index in non-array initializer");
8571 else if (tree_int_cst_sgn (first) == -1)
8572 error_init (loc, "array index in initializer exceeds array bounds");
8573 else if (constructor_max_index
8574 && tree_int_cst_lt (constructor_max_index, first))
8575 error_init (loc, "array index in initializer exceeds array bounds");
8576 else
8577 {
8578 constant_expression_warning (first);
8579 if (last)
8580 constant_expression_warning (last);
8581 constructor_index = convert (bitsizetype, first);
8582 if (tree_int_cst_lt (constructor_index, first))
8583 {
8584 constructor_index = copy_node (constructor_index);
8585 TREE_OVERFLOW (constructor_index) = 1;
8586 }
8587
8588 if (last)
8589 {
8590 if (tree_int_cst_equal (first, last))
8591 last = NULL_TREE;
8592 else if (tree_int_cst_lt (last, first))
8593 {
8594 error_init (loc, "empty index range in initializer");
8595 last = NULL_TREE;
8596 }
8597 else
8598 {
8599 last = convert (bitsizetype, last);
8600 if (constructor_max_index != NULL_TREE
8601 && tree_int_cst_lt (constructor_max_index, last))
8602 {
8603 error_init (loc, "array index range in initializer exceeds "
8604 "array bounds");
8605 last = NULL_TREE;
8606 }
8607 }
8608 }
8609
8610 designator_depth++;
8611 designator_erroneous = 0;
8612 if (constructor_range_stack || last)
8613 push_range_stack (last, braced_init_obstack);
8614 }
8615 }
8616
8617 /* Within a struct initializer, specify the next field to be initialized. */
8618
8619 void
set_init_label(location_t loc,tree fieldname,location_t fieldname_loc,struct obstack * braced_init_obstack)8620 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8621 struct obstack *braced_init_obstack)
8622 {
8623 tree field;
8624
8625 if (set_designator (loc, false, braced_init_obstack))
8626 return;
8627
8628 designator_erroneous = 1;
8629
8630 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8631 {
8632 error_init (loc, "field name not in record or union initializer");
8633 return;
8634 }
8635
8636 field = lookup_field (constructor_type, fieldname);
8637
8638 if (field == NULL_TREE)
8639 {
8640 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8641 if (guessed_id)
8642 {
8643 gcc_rich_location rich_loc (fieldname_loc);
8644 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8645 error_at (&rich_loc,
8646 "%qT has no member named %qE; did you mean %qE?",
8647 constructor_type, fieldname, guessed_id);
8648 }
8649 else
8650 error_at (fieldname_loc, "%qT has no member named %qE",
8651 constructor_type, fieldname);
8652 }
8653 else
8654 do
8655 {
8656 constructor_fields = TREE_VALUE (field);
8657 designator_depth++;
8658 designator_erroneous = 0;
8659 if (constructor_range_stack)
8660 push_range_stack (NULL_TREE, braced_init_obstack);
8661 field = TREE_CHAIN (field);
8662 if (field)
8663 {
8664 if (set_designator (loc, false, braced_init_obstack))
8665 return;
8666 }
8667 }
8668 while (field != NULL_TREE);
8669 }
8670
8671 /* Add a new initializer to the tree of pending initializers. PURPOSE
8672 identifies the initializer, either array index or field in a structure.
8673 VALUE is the value of that index or field. If ORIGTYPE is not
8674 NULL_TREE, it is the original type of VALUE.
8675
8676 IMPLICIT is true if value comes from pop_init_level (1),
8677 the new initializer has been merged with the existing one
8678 and thus no warnings should be emitted about overriding an
8679 existing initializer. */
8680
8681 static void
add_pending_init(location_t loc,tree purpose,tree value,tree origtype,bool implicit,struct obstack * braced_init_obstack)8682 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8683 bool implicit, struct obstack *braced_init_obstack)
8684 {
8685 struct init_node *p, **q, *r;
8686
8687 q = &constructor_pending_elts;
8688 p = 0;
8689
8690 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8691 {
8692 while (*q != 0)
8693 {
8694 p = *q;
8695 if (tree_int_cst_lt (purpose, p->purpose))
8696 q = &p->left;
8697 else if (tree_int_cst_lt (p->purpose, purpose))
8698 q = &p->right;
8699 else
8700 {
8701 if (!implicit)
8702 {
8703 if (TREE_SIDE_EFFECTS (p->value))
8704 warning_init (loc, OPT_Woverride_init_side_effects,
8705 "initialized field with side-effects "
8706 "overwritten");
8707 else if (warn_override_init)
8708 warning_init (loc, OPT_Woverride_init,
8709 "initialized field overwritten");
8710 }
8711 p->value = value;
8712 p->origtype = origtype;
8713 return;
8714 }
8715 }
8716 }
8717 else
8718 {
8719 tree bitpos;
8720
8721 bitpos = bit_position (purpose);
8722 while (*q != NULL)
8723 {
8724 p = *q;
8725 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8726 q = &p->left;
8727 else if (p->purpose != purpose)
8728 q = &p->right;
8729 else
8730 {
8731 if (!implicit)
8732 {
8733 if (TREE_SIDE_EFFECTS (p->value))
8734 warning_init (loc, OPT_Woverride_init_side_effects,
8735 "initialized field with side-effects "
8736 "overwritten");
8737 else if (warn_override_init)
8738 warning_init (loc, OPT_Woverride_init,
8739 "initialized field overwritten");
8740 }
8741 p->value = value;
8742 p->origtype = origtype;
8743 return;
8744 }
8745 }
8746 }
8747
8748 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8749 sizeof (struct init_node));
8750 r->purpose = purpose;
8751 r->value = value;
8752 r->origtype = origtype;
8753
8754 *q = r;
8755 r->parent = p;
8756 r->left = 0;
8757 r->right = 0;
8758 r->balance = 0;
8759
8760 while (p)
8761 {
8762 struct init_node *s;
8763
8764 if (r == p->left)
8765 {
8766 if (p->balance == 0)
8767 p->balance = -1;
8768 else if (p->balance < 0)
8769 {
8770 if (r->balance < 0)
8771 {
8772 /* L rotation. */
8773 p->left = r->right;
8774 if (p->left)
8775 p->left->parent = p;
8776 r->right = p;
8777
8778 p->balance = 0;
8779 r->balance = 0;
8780
8781 s = p->parent;
8782 p->parent = r;
8783 r->parent = s;
8784 if (s)
8785 {
8786 if (s->left == p)
8787 s->left = r;
8788 else
8789 s->right = r;
8790 }
8791 else
8792 constructor_pending_elts = r;
8793 }
8794 else
8795 {
8796 /* LR rotation. */
8797 struct init_node *t = r->right;
8798
8799 r->right = t->left;
8800 if (r->right)
8801 r->right->parent = r;
8802 t->left = r;
8803
8804 p->left = t->right;
8805 if (p->left)
8806 p->left->parent = p;
8807 t->right = p;
8808
8809 p->balance = t->balance < 0;
8810 r->balance = -(t->balance > 0);
8811 t->balance = 0;
8812
8813 s = p->parent;
8814 p->parent = t;
8815 r->parent = t;
8816 t->parent = s;
8817 if (s)
8818 {
8819 if (s->left == p)
8820 s->left = t;
8821 else
8822 s->right = t;
8823 }
8824 else
8825 constructor_pending_elts = t;
8826 }
8827 break;
8828 }
8829 else
8830 {
8831 /* p->balance == +1; growth of left side balances the node. */
8832 p->balance = 0;
8833 break;
8834 }
8835 }
8836 else /* r == p->right */
8837 {
8838 if (p->balance == 0)
8839 /* Growth propagation from right side. */
8840 p->balance++;
8841 else if (p->balance > 0)
8842 {
8843 if (r->balance > 0)
8844 {
8845 /* R rotation. */
8846 p->right = r->left;
8847 if (p->right)
8848 p->right->parent = p;
8849 r->left = p;
8850
8851 p->balance = 0;
8852 r->balance = 0;
8853
8854 s = p->parent;
8855 p->parent = r;
8856 r->parent = s;
8857 if (s)
8858 {
8859 if (s->left == p)
8860 s->left = r;
8861 else
8862 s->right = r;
8863 }
8864 else
8865 constructor_pending_elts = r;
8866 }
8867 else /* r->balance == -1 */
8868 {
8869 /* RL rotation */
8870 struct init_node *t = r->left;
8871
8872 r->left = t->right;
8873 if (r->left)
8874 r->left->parent = r;
8875 t->right = r;
8876
8877 p->right = t->left;
8878 if (p->right)
8879 p->right->parent = p;
8880 t->left = p;
8881
8882 r->balance = (t->balance < 0);
8883 p->balance = -(t->balance > 0);
8884 t->balance = 0;
8885
8886 s = p->parent;
8887 p->parent = t;
8888 r->parent = t;
8889 t->parent = s;
8890 if (s)
8891 {
8892 if (s->left == p)
8893 s->left = t;
8894 else
8895 s->right = t;
8896 }
8897 else
8898 constructor_pending_elts = t;
8899 }
8900 break;
8901 }
8902 else
8903 {
8904 /* p->balance == -1; growth of right side balances the node. */
8905 p->balance = 0;
8906 break;
8907 }
8908 }
8909
8910 r = p;
8911 p = p->parent;
8912 }
8913 }
8914
8915 /* Build AVL tree from a sorted chain. */
8916
8917 static void
set_nonincremental_init(struct obstack * braced_init_obstack)8918 set_nonincremental_init (struct obstack * braced_init_obstack)
8919 {
8920 unsigned HOST_WIDE_INT ix;
8921 tree index, value;
8922
8923 if (TREE_CODE (constructor_type) != RECORD_TYPE
8924 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8925 return;
8926
8927 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8928 add_pending_init (input_location, index, value, NULL_TREE, true,
8929 braced_init_obstack);
8930 constructor_elements = NULL;
8931 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8932 {
8933 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8934 /* Skip any nameless bit fields at the beginning. */
8935 while (constructor_unfilled_fields != NULL_TREE
8936 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
8937 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8938
8939 }
8940 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8941 {
8942 if (TYPE_DOMAIN (constructor_type))
8943 constructor_unfilled_index
8944 = convert (bitsizetype,
8945 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8946 else
8947 constructor_unfilled_index = bitsize_zero_node;
8948 }
8949 constructor_incremental = 0;
8950 }
8951
8952 /* Build AVL tree from a string constant. */
8953
8954 static void
set_nonincremental_init_from_string(tree str,struct obstack * braced_init_obstack)8955 set_nonincremental_init_from_string (tree str,
8956 struct obstack * braced_init_obstack)
8957 {
8958 tree value, purpose, type;
8959 HOST_WIDE_INT val[2];
8960 const char *p, *end;
8961 int byte, wchar_bytes, charwidth, bitpos;
8962
8963 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8964
8965 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8966 charwidth = TYPE_PRECISION (char_type_node);
8967 gcc_assert ((size_t) wchar_bytes * charwidth
8968 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
8969 type = TREE_TYPE (constructor_type);
8970 p = TREE_STRING_POINTER (str);
8971 end = p + TREE_STRING_LENGTH (str);
8972
8973 for (purpose = bitsize_zero_node;
8974 p < end
8975 && !(constructor_max_index
8976 && tree_int_cst_lt (constructor_max_index, purpose));
8977 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8978 {
8979 if (wchar_bytes == 1)
8980 {
8981 val[0] = (unsigned char) *p++;
8982 val[1] = 0;
8983 }
8984 else
8985 {
8986 val[1] = 0;
8987 val[0] = 0;
8988 for (byte = 0; byte < wchar_bytes; byte++)
8989 {
8990 if (BYTES_BIG_ENDIAN)
8991 bitpos = (wchar_bytes - byte - 1) * charwidth;
8992 else
8993 bitpos = byte * charwidth;
8994 val[bitpos / HOST_BITS_PER_WIDE_INT]
8995 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8996 << (bitpos % HOST_BITS_PER_WIDE_INT);
8997 }
8998 }
8999
9000 if (!TYPE_UNSIGNED (type))
9001 {
9002 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9003 if (bitpos < HOST_BITS_PER_WIDE_INT)
9004 {
9005 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9006 {
9007 val[0] |= HOST_WIDE_INT_M1U << bitpos;
9008 val[1] = -1;
9009 }
9010 }
9011 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9012 {
9013 if (val[0] < 0)
9014 val[1] = -1;
9015 }
9016 else if (val[1] & (HOST_WIDE_INT_1
9017 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9018 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9019 }
9020
9021 value = wide_int_to_tree (type,
9022 wide_int::from_array (val, 2,
9023 HOST_BITS_PER_WIDE_INT * 2));
9024 add_pending_init (input_location, purpose, value, NULL_TREE, true,
9025 braced_init_obstack);
9026 }
9027
9028 constructor_incremental = 0;
9029 }
9030
9031 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9032 not initialized yet. */
9033
9034 static tree
find_init_member(tree field,struct obstack * braced_init_obstack)9035 find_init_member (tree field, struct obstack * braced_init_obstack)
9036 {
9037 struct init_node *p;
9038
9039 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9040 {
9041 if (constructor_incremental
9042 && tree_int_cst_lt (field, constructor_unfilled_index))
9043 set_nonincremental_init (braced_init_obstack);
9044
9045 p = constructor_pending_elts;
9046 while (p)
9047 {
9048 if (tree_int_cst_lt (field, p->purpose))
9049 p = p->left;
9050 else if (tree_int_cst_lt (p->purpose, field))
9051 p = p->right;
9052 else
9053 return p->value;
9054 }
9055 }
9056 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9057 {
9058 tree bitpos = bit_position (field);
9059
9060 if (constructor_incremental
9061 && (!constructor_unfilled_fields
9062 || tree_int_cst_lt (bitpos,
9063 bit_position (constructor_unfilled_fields))))
9064 set_nonincremental_init (braced_init_obstack);
9065
9066 p = constructor_pending_elts;
9067 while (p)
9068 {
9069 if (field == p->purpose)
9070 return p->value;
9071 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9072 p = p->left;
9073 else
9074 p = p->right;
9075 }
9076 }
9077 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9078 {
9079 if (!vec_safe_is_empty (constructor_elements)
9080 && (constructor_elements->last ().index == field))
9081 return constructor_elements->last ().value;
9082 }
9083 return NULL_TREE;
9084 }
9085
9086 /* "Output" the next constructor element.
9087 At top level, really output it to assembler code now.
9088 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9089 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9090 TYPE is the data type that the containing data type wants here.
9091 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9092 If VALUE is a string constant, STRICT_STRING is true if it is
9093 unparenthesized or we should not warn here for it being parenthesized.
9094 For other types of VALUE, STRICT_STRING is not used.
9095
9096 PENDING if true means output pending elements that belong
9097 right after this element. (PENDING is normally true;
9098 it is false while outputting pending elements, to avoid recursion.)
9099
9100 IMPLICIT is true if value comes from pop_init_level (1),
9101 the new initializer has been merged with the existing one
9102 and thus no warnings should be emitted about overriding an
9103 existing initializer. */
9104
9105 static void
output_init_element(location_t loc,tree value,tree origtype,bool strict_string,tree type,tree field,bool pending,bool implicit,struct obstack * braced_init_obstack)9106 output_init_element (location_t loc, tree value, tree origtype,
9107 bool strict_string, tree type, tree field, bool pending,
9108 bool implicit, struct obstack * braced_init_obstack)
9109 {
9110 tree semantic_type = NULL_TREE;
9111 bool maybe_const = true;
9112 bool npc;
9113
9114 if (type == error_mark_node || value == error_mark_node)
9115 {
9116 constructor_erroneous = 1;
9117 return;
9118 }
9119 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9120 && (TREE_CODE (value) == STRING_CST
9121 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9122 && !(TREE_CODE (value) == STRING_CST
9123 && TREE_CODE (type) == ARRAY_TYPE
9124 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9125 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9126 TYPE_MAIN_VARIANT (type)))
9127 value = array_to_pointer_conversion (input_location, value);
9128
9129 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9130 && require_constant_value && pending)
9131 {
9132 /* As an extension, allow initializing objects with static storage
9133 duration with compound literals (which are then treated just as
9134 the brace enclosed list they contain). */
9135 if (flag_isoc99)
9136 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9137 "constant");
9138 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9139 value = DECL_INITIAL (decl);
9140 }
9141
9142 npc = null_pointer_constant_p (value);
9143 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9144 {
9145 semantic_type = TREE_TYPE (value);
9146 value = TREE_OPERAND (value, 0);
9147 }
9148 value = c_fully_fold (value, require_constant_value, &maybe_const);
9149
9150 if (value == error_mark_node)
9151 constructor_erroneous = 1;
9152 else if (!TREE_CONSTANT (value))
9153 constructor_constant = 0;
9154 else if (!initializer_constant_valid_p (value,
9155 TREE_TYPE (value),
9156 AGGREGATE_TYPE_P (constructor_type)
9157 && TYPE_REVERSE_STORAGE_ORDER
9158 (constructor_type))
9159 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9160 && DECL_C_BIT_FIELD (field)
9161 && TREE_CODE (value) != INTEGER_CST))
9162 constructor_simple = 0;
9163 if (!maybe_const)
9164 constructor_nonconst = 1;
9165
9166 /* Digest the initializer and issue any errors about incompatible
9167 types before issuing errors about non-constant initializers. */
9168 tree new_value = value;
9169 if (semantic_type)
9170 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9171 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9172 require_constant_value);
9173 if (new_value == error_mark_node)
9174 {
9175 constructor_erroneous = 1;
9176 return;
9177 }
9178 if (require_constant_value || require_constant_elements)
9179 constant_expression_warning (new_value);
9180
9181 /* Proceed to check the constness of the original initializer. */
9182 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9183 {
9184 if (require_constant_value)
9185 {
9186 error_init (loc, "initializer element is not constant");
9187 value = error_mark_node;
9188 }
9189 else if (require_constant_elements)
9190 pedwarn (loc, OPT_Wpedantic,
9191 "initializer element is not computable at load time");
9192 }
9193 else if (!maybe_const
9194 && (require_constant_value || require_constant_elements))
9195 pedwarn_init (loc, OPT_Wpedantic,
9196 "initializer element is not a constant expression");
9197
9198 /* Issue -Wc++-compat warnings about initializing a bitfield with
9199 enum type. */
9200 if (warn_cxx_compat
9201 && field != NULL_TREE
9202 && TREE_CODE (field) == FIELD_DECL
9203 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9204 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9205 != TYPE_MAIN_VARIANT (type))
9206 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9207 {
9208 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9209 if (checktype != error_mark_node
9210 && (TYPE_MAIN_VARIANT (checktype)
9211 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9212 warning_init (loc, OPT_Wc___compat,
9213 "enum conversion in initialization is invalid in C++");
9214 }
9215
9216 /* If this field is empty and does not have side effects (and is not at
9217 the end of structure), don't do anything other than checking the
9218 initializer. */
9219 if (field
9220 && (TREE_TYPE (field) == error_mark_node
9221 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9222 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9223 && !TREE_SIDE_EFFECTS (new_value)
9224 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9225 || DECL_CHAIN (field)))))
9226 return;
9227
9228 /* Finally, set VALUE to the initializer value digested above. */
9229 value = new_value;
9230
9231 /* If this element doesn't come next in sequence,
9232 put it on constructor_pending_elts. */
9233 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9234 && (!constructor_incremental
9235 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9236 {
9237 if (constructor_incremental
9238 && tree_int_cst_lt (field, constructor_unfilled_index))
9239 set_nonincremental_init (braced_init_obstack);
9240
9241 add_pending_init (loc, field, value, origtype, implicit,
9242 braced_init_obstack);
9243 return;
9244 }
9245 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9246 && (!constructor_incremental
9247 || field != constructor_unfilled_fields))
9248 {
9249 /* We do this for records but not for unions. In a union,
9250 no matter which field is specified, it can be initialized
9251 right away since it starts at the beginning of the union. */
9252 if (constructor_incremental)
9253 {
9254 if (!constructor_unfilled_fields)
9255 set_nonincremental_init (braced_init_obstack);
9256 else
9257 {
9258 tree bitpos, unfillpos;
9259
9260 bitpos = bit_position (field);
9261 unfillpos = bit_position (constructor_unfilled_fields);
9262
9263 if (tree_int_cst_lt (bitpos, unfillpos))
9264 set_nonincremental_init (braced_init_obstack);
9265 }
9266 }
9267
9268 add_pending_init (loc, field, value, origtype, implicit,
9269 braced_init_obstack);
9270 return;
9271 }
9272 else if (TREE_CODE (constructor_type) == UNION_TYPE
9273 && !vec_safe_is_empty (constructor_elements))
9274 {
9275 if (!implicit)
9276 {
9277 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9278 warning_init (loc, OPT_Woverride_init_side_effects,
9279 "initialized field with side-effects overwritten");
9280 else if (warn_override_init)
9281 warning_init (loc, OPT_Woverride_init,
9282 "initialized field overwritten");
9283 }
9284
9285 /* We can have just one union field set. */
9286 constructor_elements = NULL;
9287 }
9288
9289 /* Otherwise, output this element either to
9290 constructor_elements or to the assembler file. */
9291
9292 constructor_elt celt = {field, value};
9293 vec_safe_push (constructor_elements, celt);
9294
9295 /* Advance the variable that indicates sequential elements output. */
9296 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9297 constructor_unfilled_index
9298 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9299 bitsize_one_node);
9300 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9301 {
9302 constructor_unfilled_fields
9303 = DECL_CHAIN (constructor_unfilled_fields);
9304
9305 /* Skip any nameless bit fields. */
9306 while (constructor_unfilled_fields != NULL_TREE
9307 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9308 constructor_unfilled_fields =
9309 DECL_CHAIN (constructor_unfilled_fields);
9310 }
9311 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9312 constructor_unfilled_fields = NULL_TREE;
9313
9314 /* Now output any pending elements which have become next. */
9315 if (pending)
9316 output_pending_init_elements (0, braced_init_obstack);
9317 }
9318
9319 /* For two FIELD_DECLs in the same chain, return -1 if field1
9320 comes before field2, 1 if field1 comes after field2 and
9321 0 if field1 == field2. */
9322
9323 static int
init_field_decl_cmp(tree field1,tree field2)9324 init_field_decl_cmp (tree field1, tree field2)
9325 {
9326 if (field1 == field2)
9327 return 0;
9328
9329 tree bitpos1 = bit_position (field1);
9330 tree bitpos2 = bit_position (field2);
9331 if (tree_int_cst_equal (bitpos1, bitpos2))
9332 {
9333 /* If one of the fields has non-zero bitsize, then that
9334 field must be the last one in a sequence of zero
9335 sized fields, fields after it will have bigger
9336 bit_position. */
9337 if (TREE_TYPE (field1) != error_mark_node
9338 && COMPLETE_TYPE_P (TREE_TYPE (field1))
9339 && integer_nonzerop (TREE_TYPE (field1)))
9340 return 1;
9341 if (TREE_TYPE (field2) != error_mark_node
9342 && COMPLETE_TYPE_P (TREE_TYPE (field2))
9343 && integer_nonzerop (TREE_TYPE (field2)))
9344 return -1;
9345 /* Otherwise, fallback to DECL_CHAIN walk to find out
9346 which field comes earlier. Walk chains of both
9347 fields, so that if field1 and field2 are close to each
9348 other in either order, it is found soon even for large
9349 sequences of zero sized fields. */
9350 tree f1 = field1, f2 = field2;
9351 while (1)
9352 {
9353 f1 = DECL_CHAIN (f1);
9354 f2 = DECL_CHAIN (f2);
9355 if (f1 == NULL_TREE)
9356 {
9357 gcc_assert (f2);
9358 return 1;
9359 }
9360 if (f2 == NULL_TREE)
9361 return -1;
9362 if (f1 == field2)
9363 return -1;
9364 if (f2 == field1)
9365 return 1;
9366 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
9367 return 1;
9368 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
9369 return -1;
9370 }
9371 }
9372 else if (tree_int_cst_lt (bitpos1, bitpos2))
9373 return -1;
9374 else
9375 return 1;
9376 }
9377
9378 /* Output any pending elements which have become next.
9379 As we output elements, constructor_unfilled_{fields,index}
9380 advances, which may cause other elements to become next;
9381 if so, they too are output.
9382
9383 If ALL is 0, we return when there are
9384 no more pending elements to output now.
9385
9386 If ALL is 1, we output space as necessary so that
9387 we can output all the pending elements. */
9388 static void
output_pending_init_elements(int all,struct obstack * braced_init_obstack)9389 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9390 {
9391 struct init_node *elt = constructor_pending_elts;
9392 tree next;
9393
9394 retry:
9395
9396 /* Look through the whole pending tree.
9397 If we find an element that should be output now,
9398 output it. Otherwise, set NEXT to the element
9399 that comes first among those still pending. */
9400
9401 next = NULL_TREE;
9402 while (elt)
9403 {
9404 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9405 {
9406 if (tree_int_cst_equal (elt->purpose,
9407 constructor_unfilled_index))
9408 output_init_element (input_location, elt->value, elt->origtype,
9409 true, TREE_TYPE (constructor_type),
9410 constructor_unfilled_index, false, false,
9411 braced_init_obstack);
9412 else if (tree_int_cst_lt (constructor_unfilled_index,
9413 elt->purpose))
9414 {
9415 /* Advance to the next smaller node. */
9416 if (elt->left)
9417 elt = elt->left;
9418 else
9419 {
9420 /* We have reached the smallest node bigger than the
9421 current unfilled index. Fill the space first. */
9422 next = elt->purpose;
9423 break;
9424 }
9425 }
9426 else
9427 {
9428 /* Advance to the next bigger node. */
9429 if (elt->right)
9430 elt = elt->right;
9431 else
9432 {
9433 /* We have reached the biggest node in a subtree. Find
9434 the parent of it, which is the next bigger node. */
9435 while (elt->parent && elt->parent->right == elt)
9436 elt = elt->parent;
9437 elt = elt->parent;
9438 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9439 elt->purpose))
9440 {
9441 next = elt->purpose;
9442 break;
9443 }
9444 }
9445 }
9446 }
9447 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9448 {
9449 /* If the current record is complete we are done. */
9450 if (constructor_unfilled_fields == NULL_TREE)
9451 break;
9452
9453 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
9454 elt->purpose);
9455 if (cmp == 0)
9456 output_init_element (input_location, elt->value, elt->origtype,
9457 true, TREE_TYPE (elt->purpose),
9458 elt->purpose, false, false,
9459 braced_init_obstack);
9460 else if (cmp < 0)
9461 {
9462 /* Advance to the next smaller node. */
9463 if (elt->left)
9464 elt = elt->left;
9465 else
9466 {
9467 /* We have reached the smallest node bigger than the
9468 current unfilled field. Fill the space first. */
9469 next = elt->purpose;
9470 break;
9471 }
9472 }
9473 else
9474 {
9475 /* Advance to the next bigger node. */
9476 if (elt->right)
9477 elt = elt->right;
9478 else
9479 {
9480 /* We have reached the biggest node in a subtree. Find
9481 the parent of it, which is the next bigger node. */
9482 while (elt->parent && elt->parent->right == elt)
9483 elt = elt->parent;
9484 elt = elt->parent;
9485 if (elt
9486 && init_field_decl_cmp (constructor_unfilled_fields,
9487 elt->purpose) < 0)
9488 {
9489 next = elt->purpose;
9490 break;
9491 }
9492 }
9493 }
9494 }
9495 }
9496
9497 /* Ordinarily return, but not if we want to output all
9498 and there are elements left. */
9499 if (!(all && next != NULL_TREE))
9500 return;
9501
9502 /* If it's not incremental, just skip over the gap, so that after
9503 jumping to retry we will output the next successive element. */
9504 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9505 constructor_unfilled_fields = next;
9506 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9507 constructor_unfilled_index = next;
9508
9509 /* ELT now points to the node in the pending tree with the next
9510 initializer to output. */
9511 goto retry;
9512 }
9513
9514 /* Add one non-braced element to the current constructor level.
9515 This adjusts the current position within the constructor's type.
9516 This may also start or terminate implicit levels
9517 to handle a partly-braced initializer.
9518
9519 Once this has found the correct level for the new element,
9520 it calls output_init_element.
9521
9522 IMPLICIT is true if value comes from pop_init_level (1),
9523 the new initializer has been merged with the existing one
9524 and thus no warnings should be emitted about overriding an
9525 existing initializer. */
9526
9527 void
process_init_element(location_t loc,struct c_expr value,bool implicit,struct obstack * braced_init_obstack)9528 process_init_element (location_t loc, struct c_expr value, bool implicit,
9529 struct obstack * braced_init_obstack)
9530 {
9531 tree orig_value = value.value;
9532 int string_flag
9533 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
9534 bool strict_string = value.original_code == STRING_CST;
9535 bool was_designated = designator_depth != 0;
9536
9537 designator_depth = 0;
9538 designator_erroneous = 0;
9539
9540 if (!implicit && value.value && !integer_zerop (value.value))
9541 constructor_zeroinit = 0;
9542
9543 /* Handle superfluous braces around string cst as in
9544 char x[] = {"foo"}; */
9545 if (string_flag
9546 && constructor_type
9547 && !was_designated
9548 && TREE_CODE (constructor_type) == ARRAY_TYPE
9549 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9550 && integer_zerop (constructor_unfilled_index))
9551 {
9552 if (constructor_stack->replacement_value.value)
9553 error_init (loc, "excess elements in char array initializer");
9554 constructor_stack->replacement_value = value;
9555 return;
9556 }
9557
9558 if (constructor_stack->replacement_value.value != NULL_TREE)
9559 {
9560 error_init (loc, "excess elements in struct initializer");
9561 return;
9562 }
9563
9564 /* Ignore elements of a brace group if it is entirely superfluous
9565 and has already been diagnosed. */
9566 if (constructor_type == NULL_TREE)
9567 return;
9568
9569 if (!implicit && warn_designated_init && !was_designated
9570 && TREE_CODE (constructor_type) == RECORD_TYPE
9571 && lookup_attribute ("designated_init",
9572 TYPE_ATTRIBUTES (constructor_type)))
9573 warning_init (loc,
9574 OPT_Wdesignated_init,
9575 "positional initialization of field "
9576 "in %<struct%> declared with %<designated_init%> attribute");
9577
9578 /* If we've exhausted any levels that didn't have braces,
9579 pop them now. */
9580 while (constructor_stack->implicit)
9581 {
9582 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9583 && constructor_fields == NULL_TREE)
9584 process_init_element (loc,
9585 pop_init_level (loc, 1, braced_init_obstack,
9586 last_init_list_comma),
9587 true, braced_init_obstack);
9588 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9589 || VECTOR_TYPE_P (constructor_type))
9590 && constructor_max_index
9591 && tree_int_cst_lt (constructor_max_index,
9592 constructor_index))
9593 process_init_element (loc,
9594 pop_init_level (loc, 1, braced_init_obstack,
9595 last_init_list_comma),
9596 true, braced_init_obstack);
9597 else
9598 break;
9599 }
9600
9601 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9602 if (constructor_range_stack)
9603 {
9604 /* If value is a compound literal and we'll be just using its
9605 content, don't put it into a SAVE_EXPR. */
9606 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9607 || !require_constant_value)
9608 {
9609 tree semantic_type = NULL_TREE;
9610 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9611 {
9612 semantic_type = TREE_TYPE (value.value);
9613 value.value = TREE_OPERAND (value.value, 0);
9614 }
9615 value.value = save_expr (value.value);
9616 if (semantic_type)
9617 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9618 value.value);
9619 }
9620 }
9621
9622 while (1)
9623 {
9624 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9625 {
9626 tree fieldtype;
9627 enum tree_code fieldcode;
9628
9629 if (constructor_fields == NULL_TREE)
9630 {
9631 pedwarn_init (loc, 0, "excess elements in struct initializer");
9632 break;
9633 }
9634
9635 fieldtype = TREE_TYPE (constructor_fields);
9636 if (fieldtype != error_mark_node)
9637 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9638 fieldcode = TREE_CODE (fieldtype);
9639
9640 /* Error for non-static initialization of a flexible array member. */
9641 if (fieldcode == ARRAY_TYPE
9642 && !require_constant_value
9643 && TYPE_SIZE (fieldtype) == NULL_TREE
9644 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9645 {
9646 error_init (loc, "non-static initialization of a flexible "
9647 "array member");
9648 break;
9649 }
9650
9651 /* Error for initialization of a flexible array member with
9652 a string constant if the structure is in an array. E.g.:
9653 struct S { int x; char y[]; };
9654 struct S s[] = { { 1, "foo" } };
9655 is invalid. */
9656 if (string_flag
9657 && fieldcode == ARRAY_TYPE
9658 && constructor_depth > 1
9659 && TYPE_SIZE (fieldtype) == NULL_TREE
9660 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9661 {
9662 bool in_array_p = false;
9663 for (struct constructor_stack *p = constructor_stack;
9664 p && p->type; p = p->next)
9665 if (TREE_CODE (p->type) == ARRAY_TYPE)
9666 {
9667 in_array_p = true;
9668 break;
9669 }
9670 if (in_array_p)
9671 {
9672 error_init (loc, "initialization of flexible array "
9673 "member in a nested context");
9674 break;
9675 }
9676 }
9677
9678 /* Accept a string constant to initialize a subarray. */
9679 if (value.value != NULL_TREE
9680 && fieldcode == ARRAY_TYPE
9681 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9682 && string_flag)
9683 value.value = orig_value;
9684 /* Otherwise, if we have come to a subaggregate,
9685 and we don't have an element of its type, push into it. */
9686 else if (value.value != NULL_TREE
9687 && value.value != error_mark_node
9688 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9689 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9690 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9691 {
9692 push_init_level (loc, 1, braced_init_obstack);
9693 continue;
9694 }
9695
9696 if (value.value)
9697 {
9698 push_member_name (constructor_fields);
9699 output_init_element (loc, value.value, value.original_type,
9700 strict_string, fieldtype,
9701 constructor_fields, true, implicit,
9702 braced_init_obstack);
9703 RESTORE_SPELLING_DEPTH (constructor_depth);
9704 }
9705 else
9706 /* Do the bookkeeping for an element that was
9707 directly output as a constructor. */
9708 {
9709 /* For a record, keep track of end position of last field. */
9710 if (DECL_SIZE (constructor_fields))
9711 constructor_bit_index
9712 = size_binop_loc (input_location, PLUS_EXPR,
9713 bit_position (constructor_fields),
9714 DECL_SIZE (constructor_fields));
9715
9716 /* If the current field was the first one not yet written out,
9717 it isn't now, so update. */
9718 if (constructor_unfilled_fields == constructor_fields)
9719 {
9720 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9721 /* Skip any nameless bit fields. */
9722 while (constructor_unfilled_fields != 0
9723 && (DECL_UNNAMED_BIT_FIELD
9724 (constructor_unfilled_fields)))
9725 constructor_unfilled_fields =
9726 DECL_CHAIN (constructor_unfilled_fields);
9727 }
9728 }
9729
9730 constructor_fields = DECL_CHAIN (constructor_fields);
9731 /* Skip any nameless bit fields at the beginning. */
9732 while (constructor_fields != NULL_TREE
9733 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
9734 constructor_fields = DECL_CHAIN (constructor_fields);
9735 }
9736 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9737 {
9738 tree fieldtype;
9739 enum tree_code fieldcode;
9740
9741 if (constructor_fields == NULL_TREE)
9742 {
9743 pedwarn_init (loc, 0,
9744 "excess elements in union initializer");
9745 break;
9746 }
9747
9748 fieldtype = TREE_TYPE (constructor_fields);
9749 if (fieldtype != error_mark_node)
9750 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9751 fieldcode = TREE_CODE (fieldtype);
9752
9753 /* Warn that traditional C rejects initialization of unions.
9754 We skip the warning if the value is zero. This is done
9755 under the assumption that the zero initializer in user
9756 code appears conditioned on e.g. __STDC__ to avoid
9757 "missing initializer" warnings and relies on default
9758 initialization to zero in the traditional C case.
9759 We also skip the warning if the initializer is designated,
9760 again on the assumption that this must be conditional on
9761 __STDC__ anyway (and we've already complained about the
9762 member-designator already). */
9763 if (!in_system_header_at (input_location) && !constructor_designated
9764 && !(value.value && (integer_zerop (value.value)
9765 || real_zerop (value.value))))
9766 warning (OPT_Wtraditional, "traditional C rejects initialization "
9767 "of unions");
9768
9769 /* Accept a string constant to initialize a subarray. */
9770 if (value.value != NULL_TREE
9771 && fieldcode == ARRAY_TYPE
9772 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9773 && string_flag)
9774 value.value = orig_value;
9775 /* Otherwise, if we have come to a subaggregate,
9776 and we don't have an element of its type, push into it. */
9777 else if (value.value != NULL_TREE
9778 && value.value != error_mark_node
9779 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9780 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9781 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9782 {
9783 push_init_level (loc, 1, braced_init_obstack);
9784 continue;
9785 }
9786
9787 if (value.value)
9788 {
9789 push_member_name (constructor_fields);
9790 output_init_element (loc, value.value, value.original_type,
9791 strict_string, fieldtype,
9792 constructor_fields, true, implicit,
9793 braced_init_obstack);
9794 RESTORE_SPELLING_DEPTH (constructor_depth);
9795 }
9796 else
9797 /* Do the bookkeeping for an element that was
9798 directly output as a constructor. */
9799 {
9800 constructor_bit_index = DECL_SIZE (constructor_fields);
9801 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9802 }
9803
9804 constructor_fields = NULL_TREE;
9805 }
9806 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9807 {
9808 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9809 enum tree_code eltcode = TREE_CODE (elttype);
9810
9811 /* Accept a string constant to initialize a subarray. */
9812 if (value.value != NULL_TREE
9813 && eltcode == ARRAY_TYPE
9814 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9815 && string_flag)
9816 value.value = orig_value;
9817 /* Otherwise, if we have come to a subaggregate,
9818 and we don't have an element of its type, push into it. */
9819 else if (value.value != NULL_TREE
9820 && value.value != error_mark_node
9821 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9822 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9823 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9824 {
9825 push_init_level (loc, 1, braced_init_obstack);
9826 continue;
9827 }
9828
9829 if (constructor_max_index != NULL_TREE
9830 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9831 || integer_all_onesp (constructor_max_index)))
9832 {
9833 pedwarn_init (loc, 0,
9834 "excess elements in array initializer");
9835 break;
9836 }
9837
9838 /* Now output the actual element. */
9839 if (value.value)
9840 {
9841 push_array_bounds (tree_to_uhwi (constructor_index));
9842 output_init_element (loc, value.value, value.original_type,
9843 strict_string, elttype,
9844 constructor_index, true, implicit,
9845 braced_init_obstack);
9846 RESTORE_SPELLING_DEPTH (constructor_depth);
9847 }
9848
9849 constructor_index
9850 = size_binop_loc (input_location, PLUS_EXPR,
9851 constructor_index, bitsize_one_node);
9852
9853 if (!value.value)
9854 /* If we are doing the bookkeeping for an element that was
9855 directly output as a constructor, we must update
9856 constructor_unfilled_index. */
9857 constructor_unfilled_index = constructor_index;
9858 }
9859 else if (VECTOR_TYPE_P (constructor_type))
9860 {
9861 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9862
9863 /* Do a basic check of initializer size. Note that vectors
9864 always have a fixed size derived from their type. */
9865 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9866 {
9867 pedwarn_init (loc, 0,
9868 "excess elements in vector initializer");
9869 break;
9870 }
9871
9872 /* Now output the actual element. */
9873 if (value.value)
9874 {
9875 if (TREE_CODE (value.value) == VECTOR_CST)
9876 elttype = TYPE_MAIN_VARIANT (constructor_type);
9877 output_init_element (loc, value.value, value.original_type,
9878 strict_string, elttype,
9879 constructor_index, true, implicit,
9880 braced_init_obstack);
9881 }
9882
9883 constructor_index
9884 = size_binop_loc (input_location,
9885 PLUS_EXPR, constructor_index, bitsize_one_node);
9886
9887 if (!value.value)
9888 /* If we are doing the bookkeeping for an element that was
9889 directly output as a constructor, we must update
9890 constructor_unfilled_index. */
9891 constructor_unfilled_index = constructor_index;
9892 }
9893
9894 /* Handle the sole element allowed in a braced initializer
9895 for a scalar variable. */
9896 else if (constructor_type != error_mark_node
9897 && constructor_fields == NULL_TREE)
9898 {
9899 pedwarn_init (loc, 0,
9900 "excess elements in scalar initializer");
9901 break;
9902 }
9903 else
9904 {
9905 if (value.value)
9906 output_init_element (loc, value.value, value.original_type,
9907 strict_string, constructor_type,
9908 NULL_TREE, true, implicit,
9909 braced_init_obstack);
9910 constructor_fields = NULL_TREE;
9911 }
9912
9913 /* Handle range initializers either at this level or anywhere higher
9914 in the designator stack. */
9915 if (constructor_range_stack)
9916 {
9917 struct constructor_range_stack *p, *range_stack;
9918 int finish = 0;
9919
9920 range_stack = constructor_range_stack;
9921 constructor_range_stack = 0;
9922 while (constructor_stack != range_stack->stack)
9923 {
9924 gcc_assert (constructor_stack->implicit);
9925 process_init_element (loc,
9926 pop_init_level (loc, 1,
9927 braced_init_obstack,
9928 last_init_list_comma),
9929 true, braced_init_obstack);
9930 }
9931 for (p = range_stack;
9932 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9933 p = p->prev)
9934 {
9935 gcc_assert (constructor_stack->implicit);
9936 process_init_element (loc,
9937 pop_init_level (loc, 1,
9938 braced_init_obstack,
9939 last_init_list_comma),
9940 true, braced_init_obstack);
9941 }
9942
9943 p->index = size_binop_loc (input_location,
9944 PLUS_EXPR, p->index, bitsize_one_node);
9945 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9946 finish = 1;
9947
9948 while (1)
9949 {
9950 constructor_index = p->index;
9951 constructor_fields = p->fields;
9952 if (finish && p->range_end && p->index == p->range_start)
9953 {
9954 finish = 0;
9955 p->prev = 0;
9956 }
9957 p = p->next;
9958 if (!p)
9959 break;
9960 finish_implicit_inits (loc, braced_init_obstack);
9961 push_init_level (loc, 2, braced_init_obstack);
9962 p->stack = constructor_stack;
9963 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9964 p->index = p->range_start;
9965 }
9966
9967 if (!finish)
9968 constructor_range_stack = range_stack;
9969 continue;
9970 }
9971
9972 break;
9973 }
9974
9975 constructor_range_stack = 0;
9976 }
9977
9978 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9979 (guaranteed to be 'volatile' or null) and ARGS (represented using
9980 an ASM_EXPR node). */
9981 tree
build_asm_stmt(bool is_volatile,tree args)9982 build_asm_stmt (bool is_volatile, tree args)
9983 {
9984 if (is_volatile)
9985 ASM_VOLATILE_P (args) = 1;
9986 return add_stmt (args);
9987 }
9988
9989 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9990 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9991 SIMPLE indicates whether there was anything at all after the
9992 string in the asm expression -- asm("blah") and asm("blah" : )
9993 are subtly different. We use a ASM_EXPR node to represent this.
9994 LOC is the location of the asm, and IS_INLINE says whether this
9995 is asm inline. */
9996 tree
build_asm_expr(location_t loc,tree string,tree outputs,tree inputs,tree clobbers,tree labels,bool simple,bool is_inline)9997 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9998 tree clobbers, tree labels, bool simple, bool is_inline)
9999 {
10000 tree tail;
10001 tree args;
10002 int i;
10003 const char *constraint;
10004 const char **oconstraints;
10005 bool allows_mem, allows_reg, is_inout;
10006 int ninputs, noutputs;
10007
10008 ninputs = list_length (inputs);
10009 noutputs = list_length (outputs);
10010 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10011
10012 string = resolve_asm_operand_names (string, outputs, inputs, labels);
10013
10014 /* Remove output conversions that change the type but not the mode. */
10015 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10016 {
10017 tree output = TREE_VALUE (tail);
10018
10019 output = c_fully_fold (output, false, NULL, true);
10020
10021 /* ??? Really, this should not be here. Users should be using a
10022 proper lvalue, dammit. But there's a long history of using casts
10023 in the output operands. In cases like longlong.h, this becomes a
10024 primitive form of typechecking -- if the cast can be removed, then
10025 the output operand had a type of the proper width; otherwise we'll
10026 get an error. Gross, but ... */
10027 STRIP_NOPS (output);
10028
10029 if (!lvalue_or_else (loc, output, lv_asm))
10030 output = error_mark_node;
10031
10032 if (output != error_mark_node
10033 && (TREE_READONLY (output)
10034 || TYPE_READONLY (TREE_TYPE (output))
10035 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10036 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10037 readonly_error (loc, output, lv_asm);
10038
10039 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10040 oconstraints[i] = constraint;
10041
10042 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10043 &allows_mem, &allows_reg, &is_inout))
10044 {
10045 /* If the operand is going to end up in memory,
10046 mark it addressable. */
10047 if (!allows_reg && !c_mark_addressable (output))
10048 output = error_mark_node;
10049 if (!(!allows_reg && allows_mem)
10050 && output != error_mark_node
10051 && VOID_TYPE_P (TREE_TYPE (output)))
10052 {
10053 error_at (loc, "invalid use of void expression");
10054 output = error_mark_node;
10055 }
10056 }
10057 else
10058 output = error_mark_node;
10059
10060 TREE_VALUE (tail) = output;
10061 }
10062
10063 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10064 {
10065 tree input;
10066
10067 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10068 input = TREE_VALUE (tail);
10069
10070 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10071 oconstraints, &allows_mem, &allows_reg))
10072 {
10073 /* If the operand is going to end up in memory,
10074 mark it addressable. */
10075 if (!allows_reg && allows_mem)
10076 {
10077 input = c_fully_fold (input, false, NULL, true);
10078
10079 /* Strip the nops as we allow this case. FIXME, this really
10080 should be rejected or made deprecated. */
10081 STRIP_NOPS (input);
10082 if (!c_mark_addressable (input))
10083 input = error_mark_node;
10084 }
10085 else
10086 {
10087 struct c_expr expr;
10088 memset (&expr, 0, sizeof (expr));
10089 expr.value = input;
10090 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10091 input = c_fully_fold (expr.value, false, NULL);
10092
10093 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10094 {
10095 error_at (loc, "invalid use of void expression");
10096 input = error_mark_node;
10097 }
10098 }
10099 }
10100 else
10101 input = error_mark_node;
10102
10103 TREE_VALUE (tail) = input;
10104 }
10105
10106 /* ASMs with labels cannot have outputs. This should have been
10107 enforced by the parser. */
10108 gcc_assert (outputs == NULL || labels == NULL);
10109
10110 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10111
10112 /* asm statements without outputs, including simple ones, are treated
10113 as volatile. */
10114 ASM_INPUT_P (args) = simple;
10115 ASM_VOLATILE_P (args) = (noutputs == 0);
10116 ASM_INLINE_P (args) = is_inline;
10117
10118 return args;
10119 }
10120
10121 /* Generate a goto statement to LABEL. LOC is the location of the
10122 GOTO. */
10123
10124 tree
c_finish_goto_label(location_t loc,tree label)10125 c_finish_goto_label (location_t loc, tree label)
10126 {
10127 tree decl = lookup_label_for_goto (loc, label);
10128 if (!decl)
10129 return NULL_TREE;
10130 TREE_USED (decl) = 1;
10131 {
10132 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10133 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10134 SET_EXPR_LOCATION (t, loc);
10135 return add_stmt (t);
10136 }
10137 }
10138
10139 /* Generate a computed goto statement to EXPR. LOC is the location of
10140 the GOTO. */
10141
10142 tree
c_finish_goto_ptr(location_t loc,tree expr)10143 c_finish_goto_ptr (location_t loc, tree expr)
10144 {
10145 tree t;
10146 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10147 expr = c_fully_fold (expr, false, NULL);
10148 expr = convert (ptr_type_node, expr);
10149 t = build1 (GOTO_EXPR, void_type_node, expr);
10150 SET_EXPR_LOCATION (t, loc);
10151 return add_stmt (t);
10152 }
10153
10154 /* Generate a C `return' statement. RETVAL is the expression for what
10155 to return, or a null pointer for `return;' with no value. LOC is
10156 the location of the return statement, or the location of the expression,
10157 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10158 is the original type of RETVAL. */
10159
10160 tree
c_finish_return(location_t loc,tree retval,tree origtype)10161 c_finish_return (location_t loc, tree retval, tree origtype)
10162 {
10163 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10164 bool no_warning = false;
10165 bool npc = false;
10166
10167 /* Use the expansion point to handle cases such as returning NULL
10168 in a function returning void. */
10169 source_location xloc = expansion_point_location_if_in_system_header (loc);
10170
10171 if (TREE_THIS_VOLATILE (current_function_decl))
10172 warning_at (xloc, 0,
10173 "function declared %<noreturn%> has a %<return%> statement");
10174
10175 if (retval)
10176 {
10177 tree semantic_type = NULL_TREE;
10178 npc = null_pointer_constant_p (retval);
10179 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10180 {
10181 semantic_type = TREE_TYPE (retval);
10182 retval = TREE_OPERAND (retval, 0);
10183 }
10184 retval = c_fully_fold (retval, false, NULL);
10185 if (semantic_type)
10186 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10187 }
10188
10189 if (!retval)
10190 {
10191 current_function_returns_null = 1;
10192 if ((warn_return_type || flag_isoc99)
10193 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10194 {
10195 bool warned_here;
10196 if (flag_isoc99)
10197 warned_here = pedwarn
10198 (loc, 0,
10199 "%<return%> with no value, in function returning non-void");
10200 else
10201 warned_here = warning_at
10202 (loc, OPT_Wreturn_type,
10203 "%<return%> with no value, in function returning non-void");
10204 no_warning = true;
10205 if (warned_here)
10206 inform (DECL_SOURCE_LOCATION (current_function_decl),
10207 "declared here");
10208 }
10209 }
10210 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10211 {
10212 current_function_returns_null = 1;
10213 bool warned_here;
10214 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10215 warned_here = pedwarn
10216 (xloc, 0,
10217 "%<return%> with a value, in function returning void");
10218 else
10219 warned_here = pedwarn
10220 (xloc, OPT_Wpedantic, "ISO C forbids "
10221 "%<return%> with expression, in function returning void");
10222 if (warned_here)
10223 inform (DECL_SOURCE_LOCATION (current_function_decl),
10224 "declared here");
10225 }
10226 else
10227 {
10228 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10229 retval, origtype, ic_return,
10230 npc, NULL_TREE, NULL_TREE, 0);
10231 tree res = DECL_RESULT (current_function_decl);
10232 tree inner;
10233 bool save;
10234
10235 current_function_returns_value = 1;
10236 if (t == error_mark_node)
10237 return NULL_TREE;
10238
10239 save = in_late_binary_op;
10240 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10241 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10242 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10243 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10244 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10245 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10246 in_late_binary_op = true;
10247 inner = t = convert (TREE_TYPE (res), t);
10248 in_late_binary_op = save;
10249
10250 /* Strip any conversions, additions, and subtractions, and see if
10251 we are returning the address of a local variable. Warn if so. */
10252 while (1)
10253 {
10254 switch (TREE_CODE (inner))
10255 {
10256 CASE_CONVERT:
10257 case NON_LVALUE_EXPR:
10258 case PLUS_EXPR:
10259 case POINTER_PLUS_EXPR:
10260 inner = TREE_OPERAND (inner, 0);
10261 continue;
10262
10263 case MINUS_EXPR:
10264 /* If the second operand of the MINUS_EXPR has a pointer
10265 type (or is converted from it), this may be valid, so
10266 don't give a warning. */
10267 {
10268 tree op1 = TREE_OPERAND (inner, 1);
10269
10270 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10271 && (CONVERT_EXPR_P (op1)
10272 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10273 op1 = TREE_OPERAND (op1, 0);
10274
10275 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10276 break;
10277
10278 inner = TREE_OPERAND (inner, 0);
10279 continue;
10280 }
10281
10282 case ADDR_EXPR:
10283 inner = TREE_OPERAND (inner, 0);
10284
10285 while (REFERENCE_CLASS_P (inner)
10286 && !INDIRECT_REF_P (inner))
10287 inner = TREE_OPERAND (inner, 0);
10288
10289 if (DECL_P (inner)
10290 && !DECL_EXTERNAL (inner)
10291 && !TREE_STATIC (inner)
10292 && DECL_CONTEXT (inner) == current_function_decl)
10293 {
10294 if (TREE_CODE (inner) == LABEL_DECL)
10295 warning_at (loc, OPT_Wreturn_local_addr,
10296 "function returns address of label");
10297 else
10298 {
10299 warning_at (loc, OPT_Wreturn_local_addr,
10300 "function returns address of local variable");
10301 tree zero = build_zero_cst (TREE_TYPE (res));
10302 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10303 }
10304 }
10305 break;
10306
10307 default:
10308 break;
10309 }
10310
10311 break;
10312 }
10313
10314 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10315 SET_EXPR_LOCATION (retval, loc);
10316
10317 if (warn_sequence_point)
10318 verify_sequence_points (retval);
10319 }
10320
10321 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10322 TREE_NO_WARNING (ret_stmt) |= no_warning;
10323 return add_stmt (ret_stmt);
10324 }
10325
10326 struct c_switch {
10327 /* The SWITCH_EXPR being built. */
10328 tree switch_expr;
10329
10330 /* The original type of the testing expression, i.e. before the
10331 default conversion is applied. */
10332 tree orig_type;
10333
10334 /* A splay-tree mapping the low element of a case range to the high
10335 element, or NULL_TREE if there is no high element. Used to
10336 determine whether or not a new case label duplicates an old case
10337 label. We need a tree, rather than simply a hash table, because
10338 of the GNU case range extension. */
10339 splay_tree cases;
10340
10341 /* The bindings at the point of the switch. This is used for
10342 warnings crossing decls when branching to a case label. */
10343 struct c_spot_bindings *bindings;
10344
10345 /* The next node on the stack. */
10346 struct c_switch *next;
10347
10348 /* Remember whether the controlling expression had boolean type
10349 before integer promotions for the sake of -Wswitch-bool. */
10350 bool bool_cond_p;
10351
10352 /* Remember whether there was a case value that is outside the
10353 range of the ORIG_TYPE. */
10354 bool outside_range_p;
10355 };
10356
10357 /* A stack of the currently active switch statements. The innermost
10358 switch statement is on the top of the stack. There is no need to
10359 mark the stack for garbage collection because it is only active
10360 during the processing of the body of a function, and we never
10361 collect at that point. */
10362
10363 struct c_switch *c_switch_stack;
10364
10365 /* Start a C switch statement, testing expression EXP. Return the new
10366 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10367 SWITCH_COND_LOC is the location of the switch's condition.
10368 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10369
10370 tree
c_start_case(location_t switch_loc,location_t switch_cond_loc,tree exp,bool explicit_cast_p)10371 c_start_case (location_t switch_loc,
10372 location_t switch_cond_loc,
10373 tree exp, bool explicit_cast_p)
10374 {
10375 tree orig_type = error_mark_node;
10376 bool bool_cond_p = false;
10377 struct c_switch *cs;
10378
10379 if (exp != error_mark_node)
10380 {
10381 orig_type = TREE_TYPE (exp);
10382
10383 if (!INTEGRAL_TYPE_P (orig_type))
10384 {
10385 if (orig_type != error_mark_node)
10386 {
10387 error_at (switch_cond_loc, "switch quantity not an integer");
10388 orig_type = error_mark_node;
10389 }
10390 exp = integer_zero_node;
10391 }
10392 else
10393 {
10394 tree type = TYPE_MAIN_VARIANT (orig_type);
10395 tree e = exp;
10396
10397 /* Warn if the condition has boolean value. */
10398 while (TREE_CODE (e) == COMPOUND_EXPR)
10399 e = TREE_OPERAND (e, 1);
10400
10401 if ((TREE_CODE (type) == BOOLEAN_TYPE
10402 || truth_value_p (TREE_CODE (e)))
10403 /* Explicit cast to int suppresses this warning. */
10404 && !(TREE_CODE (type) == INTEGER_TYPE
10405 && explicit_cast_p))
10406 bool_cond_p = true;
10407
10408 if (!in_system_header_at (input_location)
10409 && (type == long_integer_type_node
10410 || type == long_unsigned_type_node))
10411 warning_at (switch_cond_loc,
10412 OPT_Wtraditional, "%<long%> switch expression not "
10413 "converted to %<int%> in ISO C");
10414
10415 exp = c_fully_fold (exp, false, NULL);
10416 exp = default_conversion (exp);
10417
10418 if (warn_sequence_point)
10419 verify_sequence_points (exp);
10420 }
10421 }
10422
10423 /* Add this new SWITCH_EXPR to the stack. */
10424 cs = XNEW (struct c_switch);
10425 cs->switch_expr = build2 (SWITCH_EXPR, orig_type, exp, NULL_TREE);
10426 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10427 cs->orig_type = orig_type;
10428 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10429 cs->bindings = c_get_switch_bindings ();
10430 cs->bool_cond_p = bool_cond_p;
10431 cs->outside_range_p = false;
10432 cs->next = c_switch_stack;
10433 c_switch_stack = cs;
10434
10435 return add_stmt (cs->switch_expr);
10436 }
10437
10438 /* Process a case label at location LOC. */
10439
10440 tree
do_case(location_t loc,tree low_value,tree high_value)10441 do_case (location_t loc, tree low_value, tree high_value)
10442 {
10443 tree label = NULL_TREE;
10444
10445 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10446 {
10447 low_value = c_fully_fold (low_value, false, NULL);
10448 if (TREE_CODE (low_value) == INTEGER_CST)
10449 pedwarn (loc, OPT_Wpedantic,
10450 "case label is not an integer constant expression");
10451 }
10452
10453 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10454 {
10455 high_value = c_fully_fold (high_value, false, NULL);
10456 if (TREE_CODE (high_value) == INTEGER_CST)
10457 pedwarn (input_location, OPT_Wpedantic,
10458 "case label is not an integer constant expression");
10459 }
10460
10461 if (c_switch_stack == NULL)
10462 {
10463 if (low_value)
10464 error_at (loc, "case label not within a switch statement");
10465 else
10466 error_at (loc, "%<default%> label not within a switch statement");
10467 return NULL_TREE;
10468 }
10469
10470 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10471 EXPR_LOCATION (c_switch_stack->switch_expr),
10472 loc))
10473 return NULL_TREE;
10474
10475 label = c_add_case_label (loc, c_switch_stack->cases,
10476 SWITCH_COND (c_switch_stack->switch_expr),
10477 c_switch_stack->orig_type,
10478 low_value, high_value,
10479 &c_switch_stack->outside_range_p);
10480 if (label == error_mark_node)
10481 label = NULL_TREE;
10482 return label;
10483 }
10484
10485 /* Finish the switch statement. TYPE is the original type of the
10486 controlling expression of the switch, or NULL_TREE. */
10487
10488 void
c_finish_case(tree body,tree type)10489 c_finish_case (tree body, tree type)
10490 {
10491 struct c_switch *cs = c_switch_stack;
10492 location_t switch_location;
10493
10494 SWITCH_BODY (cs->switch_expr) = body;
10495
10496 /* Emit warnings as needed. */
10497 switch_location = EXPR_LOCATION (cs->switch_expr);
10498 c_do_switch_warnings (cs->cases, switch_location,
10499 type ? type : TREE_TYPE (cs->switch_expr),
10500 SWITCH_COND (cs->switch_expr),
10501 cs->bool_cond_p, cs->outside_range_p);
10502 if (c_switch_covers_all_cases_p (cs->cases, TREE_TYPE (cs->switch_expr)))
10503 SWITCH_ALL_CASES_P (cs->switch_expr) = 1;
10504
10505 /* Pop the stack. */
10506 c_switch_stack = cs->next;
10507 splay_tree_delete (cs->cases);
10508 c_release_switch_bindings (cs->bindings);
10509 XDELETE (cs);
10510 }
10511
10512 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10513 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10514 may be null. */
10515
10516 void
c_finish_if_stmt(location_t if_locus,tree cond,tree then_block,tree else_block)10517 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10518 tree else_block)
10519 {
10520 tree stmt;
10521
10522 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10523 SET_EXPR_LOCATION (stmt, if_locus);
10524 add_stmt (stmt);
10525 }
10526
10527 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10528 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10529 is false for DO loops. INCR is the FOR increment expression. BODY is
10530 the statement controlled by the loop. BLAB is the break label. CLAB is
10531 the continue label. Everything is allowed to be NULL. */
10532
10533 void
c_finish_loop(location_t start_locus,tree cond,tree incr,tree body,tree blab,tree clab,bool cond_is_first)10534 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10535 tree blab, tree clab, bool cond_is_first)
10536 {
10537 tree entry = NULL, exit = NULL, t;
10538
10539 /* If the condition is zero don't generate a loop construct. */
10540 if (cond && integer_zerop (cond))
10541 {
10542 if (cond_is_first)
10543 {
10544 t = build_and_jump (&blab);
10545 SET_EXPR_LOCATION (t, start_locus);
10546 add_stmt (t);
10547 }
10548 }
10549 else
10550 {
10551 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10552
10553 /* If we have an exit condition, then we build an IF with gotos either
10554 out of the loop, or to the top of it. If there's no exit condition,
10555 then we just build a jump back to the top. */
10556 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10557
10558 if (cond && !integer_nonzerop (cond))
10559 {
10560 /* Canonicalize the loop condition to the end. This means
10561 generating a branch to the loop condition. Reuse the
10562 continue label, if possible. */
10563 if (cond_is_first)
10564 {
10565 if (incr || !clab)
10566 {
10567 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10568 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10569 }
10570 else
10571 t = build1 (GOTO_EXPR, void_type_node, clab);
10572 SET_EXPR_LOCATION (t, start_locus);
10573 add_stmt (t);
10574 }
10575
10576 t = build_and_jump (&blab);
10577 if (cond_is_first)
10578 exit = fold_build3_loc (start_locus,
10579 COND_EXPR, void_type_node, cond, exit, t);
10580 else
10581 exit = fold_build3_loc (input_location,
10582 COND_EXPR, void_type_node, cond, exit, t);
10583 }
10584 else
10585 {
10586 /* For the backward-goto's location of an unconditional loop
10587 use the beginning of the body, or, if there is none, the
10588 top of the loop. */
10589 location_t loc = EXPR_LOCATION (expr_first (body));
10590 if (loc == UNKNOWN_LOCATION)
10591 loc = start_locus;
10592 SET_EXPR_LOCATION (exit, loc);
10593 }
10594
10595 add_stmt (top);
10596 }
10597
10598 if (body)
10599 add_stmt (body);
10600 if (clab)
10601 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10602 if (incr)
10603 add_stmt (incr);
10604 if (entry)
10605 add_stmt (entry);
10606 if (exit)
10607 add_stmt (exit);
10608 if (blab)
10609 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10610 }
10611
10612 tree
c_finish_bc_stmt(location_t loc,tree * label_p,bool is_break)10613 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10614 {
10615 bool skip;
10616 tree label = *label_p;
10617
10618 /* In switch statements break is sometimes stylistically used after
10619 a return statement. This can lead to spurious warnings about
10620 control reaching the end of a non-void function when it is
10621 inlined. Note that we are calling block_may_fallthru with
10622 language specific tree nodes; this works because
10623 block_may_fallthru returns true when given something it does not
10624 understand. */
10625 skip = !block_may_fallthru (cur_stmt_list);
10626
10627 if (!label)
10628 {
10629 if (!skip)
10630 *label_p = label = create_artificial_label (loc);
10631 }
10632 else if (TREE_CODE (label) == LABEL_DECL)
10633 ;
10634 else switch (TREE_INT_CST_LOW (label))
10635 {
10636 case 0:
10637 if (is_break)
10638 error_at (loc, "break statement not within loop or switch");
10639 else
10640 error_at (loc, "continue statement not within a loop");
10641 return NULL_TREE;
10642
10643 case 1:
10644 gcc_assert (is_break);
10645 error_at (loc, "break statement used with OpenMP for loop");
10646 return NULL_TREE;
10647
10648 case 2:
10649 if (is_break)
10650 error ("break statement within %<#pragma simd%> loop body");
10651 else
10652 error ("continue statement within %<#pragma simd%> loop body");
10653 return NULL_TREE;
10654
10655 default:
10656 gcc_unreachable ();
10657 }
10658
10659 if (skip)
10660 return NULL_TREE;
10661
10662 if (!is_break)
10663 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10664
10665 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10666 }
10667
10668 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10669
10670 static void
emit_side_effect_warnings(location_t loc,tree expr)10671 emit_side_effect_warnings (location_t loc, tree expr)
10672 {
10673 if (expr == error_mark_node)
10674 ;
10675 else if (!TREE_SIDE_EFFECTS (expr))
10676 {
10677 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10678 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10679 }
10680 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10681 {
10682 tree r = expr;
10683 location_t cloc = loc;
10684 while (TREE_CODE (r) == COMPOUND_EXPR)
10685 {
10686 if (EXPR_HAS_LOCATION (r))
10687 cloc = EXPR_LOCATION (r);
10688 r = TREE_OPERAND (r, 1);
10689 }
10690 if (!TREE_SIDE_EFFECTS (r)
10691 && !VOID_TYPE_P (TREE_TYPE (r))
10692 && !CONVERT_EXPR_P (r)
10693 && !TREE_NO_WARNING (r)
10694 && !TREE_NO_WARNING (expr))
10695 warning_at (cloc, OPT_Wunused_value,
10696 "right-hand operand of comma expression has no effect");
10697 }
10698 else
10699 warn_if_unused_value (expr, loc);
10700 }
10701
10702 /* Process an expression as if it were a complete statement. Emit
10703 diagnostics, but do not call ADD_STMT. LOC is the location of the
10704 statement. */
10705
10706 tree
c_process_expr_stmt(location_t loc,tree expr)10707 c_process_expr_stmt (location_t loc, tree expr)
10708 {
10709 tree exprv;
10710
10711 if (!expr)
10712 return NULL_TREE;
10713
10714 expr = c_fully_fold (expr, false, NULL);
10715
10716 if (warn_sequence_point)
10717 verify_sequence_points (expr);
10718
10719 if (TREE_TYPE (expr) != error_mark_node
10720 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10721 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10722 error_at (loc, "expression statement has incomplete type");
10723
10724 /* If we're not processing a statement expression, warn about unused values.
10725 Warnings for statement expressions will be emitted later, once we figure
10726 out which is the result. */
10727 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10728 && warn_unused_value)
10729 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10730
10731 exprv = expr;
10732 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10733 exprv = TREE_OPERAND (exprv, 1);
10734 while (CONVERT_EXPR_P (exprv))
10735 exprv = TREE_OPERAND (exprv, 0);
10736 if (DECL_P (exprv)
10737 || handled_component_p (exprv)
10738 || TREE_CODE (exprv) == ADDR_EXPR)
10739 mark_exp_read (exprv);
10740
10741 /* If the expression is not of a type to which we cannot assign a line
10742 number, wrap the thing in a no-op NOP_EXPR. */
10743 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10744 {
10745 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10746 SET_EXPR_LOCATION (expr, loc);
10747 }
10748
10749 return expr;
10750 }
10751
10752 /* Emit an expression as a statement. LOC is the location of the
10753 expression. */
10754
10755 tree
c_finish_expr_stmt(location_t loc,tree expr)10756 c_finish_expr_stmt (location_t loc, tree expr)
10757 {
10758 if (expr)
10759 return add_stmt (c_process_expr_stmt (loc, expr));
10760 else
10761 return NULL;
10762 }
10763
10764 /* Do the opposite and emit a statement as an expression. To begin,
10765 create a new binding level and return it. */
10766
10767 tree
c_begin_stmt_expr(void)10768 c_begin_stmt_expr (void)
10769 {
10770 tree ret;
10771
10772 /* We must force a BLOCK for this level so that, if it is not expanded
10773 later, there is a way to turn off the entire subtree of blocks that
10774 are contained in it. */
10775 keep_next_level ();
10776 ret = c_begin_compound_stmt (true);
10777
10778 c_bindings_start_stmt_expr (c_switch_stack == NULL
10779 ? NULL
10780 : c_switch_stack->bindings);
10781
10782 /* Mark the current statement list as belonging to a statement list. */
10783 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10784
10785 return ret;
10786 }
10787
10788 /* LOC is the location of the compound statement to which this body
10789 belongs. */
10790
10791 tree
c_finish_stmt_expr(location_t loc,tree body)10792 c_finish_stmt_expr (location_t loc, tree body)
10793 {
10794 tree last, type, tmp, val;
10795 tree *last_p;
10796
10797 body = c_end_compound_stmt (loc, body, true);
10798
10799 c_bindings_end_stmt_expr (c_switch_stack == NULL
10800 ? NULL
10801 : c_switch_stack->bindings);
10802
10803 /* Locate the last statement in BODY. See c_end_compound_stmt
10804 about always returning a BIND_EXPR. */
10805 last_p = &BIND_EXPR_BODY (body);
10806 last = BIND_EXPR_BODY (body);
10807
10808 continue_searching:
10809 if (TREE_CODE (last) == STATEMENT_LIST)
10810 {
10811 tree_stmt_iterator l = tsi_last (last);
10812
10813 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
10814 tsi_prev (&l);
10815
10816 /* This can happen with degenerate cases like ({ }). No value. */
10817 if (tsi_end_p (l))
10818 return body;
10819
10820 /* If we're supposed to generate side effects warnings, process
10821 all of the statements except the last. */
10822 if (warn_unused_value)
10823 {
10824 for (tree_stmt_iterator i = tsi_start (last);
10825 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
10826 {
10827 location_t tloc;
10828 tree t = tsi_stmt (i);
10829
10830 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10831 emit_side_effect_warnings (tloc, t);
10832 }
10833 }
10834 last_p = tsi_stmt_ptr (l);
10835 last = *last_p;
10836 }
10837
10838 /* If the end of the list is exception related, then the list was split
10839 by a call to push_cleanup. Continue searching. */
10840 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10841 || TREE_CODE (last) == TRY_CATCH_EXPR)
10842 {
10843 last_p = &TREE_OPERAND (last, 0);
10844 last = *last_p;
10845 goto continue_searching;
10846 }
10847
10848 if (last == error_mark_node)
10849 return last;
10850
10851 /* In the case that the BIND_EXPR is not necessary, return the
10852 expression out from inside it. */
10853 if ((last == BIND_EXPR_BODY (body)
10854 /* Skip nested debug stmts. */
10855 || last == expr_first (BIND_EXPR_BODY (body)))
10856 && BIND_EXPR_VARS (body) == NULL)
10857 {
10858 /* Even if this looks constant, do not allow it in a constant
10859 expression. */
10860 last = c_wrap_maybe_const (last, true);
10861 /* Do not warn if the return value of a statement expression is
10862 unused. */
10863 TREE_NO_WARNING (last) = 1;
10864 return last;
10865 }
10866
10867 /* Extract the type of said expression. */
10868 type = TREE_TYPE (last);
10869
10870 /* If we're not returning a value at all, then the BIND_EXPR that
10871 we already have is a fine expression to return. */
10872 if (!type || VOID_TYPE_P (type))
10873 return body;
10874
10875 /* Now that we've located the expression containing the value, it seems
10876 silly to make voidify_wrapper_expr repeat the process. Create a
10877 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10878 tmp = create_tmp_var_raw (type);
10879
10880 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10881 tree_expr_nonnegative_p giving up immediately. */
10882 val = last;
10883 if (TREE_CODE (val) == NOP_EXPR
10884 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10885 val = TREE_OPERAND (val, 0);
10886
10887 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10888 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10889
10890 {
10891 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10892 SET_EXPR_LOCATION (t, loc);
10893 return t;
10894 }
10895 }
10896
10897 /* Begin and end compound statements. This is as simple as pushing
10898 and popping new statement lists from the tree. */
10899
10900 tree
c_begin_compound_stmt(bool do_scope)10901 c_begin_compound_stmt (bool do_scope)
10902 {
10903 tree stmt = push_stmt_list ();
10904 if (do_scope)
10905 push_scope ();
10906 return stmt;
10907 }
10908
10909 /* End a compound statement. STMT is the statement. LOC is the
10910 location of the compound statement-- this is usually the location
10911 of the opening brace. */
10912
10913 tree
c_end_compound_stmt(location_t loc,tree stmt,bool do_scope)10914 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10915 {
10916 tree block = NULL;
10917
10918 if (do_scope)
10919 {
10920 if (c_dialect_objc ())
10921 objc_clear_super_receiver ();
10922 block = pop_scope ();
10923 }
10924
10925 stmt = pop_stmt_list (stmt);
10926 stmt = c_build_bind_expr (loc, block, stmt);
10927
10928 /* If this compound statement is nested immediately inside a statement
10929 expression, then force a BIND_EXPR to be created. Otherwise we'll
10930 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10931 STATEMENT_LISTs merge, and thus we can lose track of what statement
10932 was really last. */
10933 if (building_stmt_list_p ()
10934 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10935 && TREE_CODE (stmt) != BIND_EXPR)
10936 {
10937 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10938 TREE_SIDE_EFFECTS (stmt) = 1;
10939 SET_EXPR_LOCATION (stmt, loc);
10940 }
10941
10942 return stmt;
10943 }
10944
10945 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10946 when the current scope is exited. EH_ONLY is true when this is not
10947 meant to apply to normal control flow transfer. */
10948
10949 void
push_cleanup(tree decl,tree cleanup,bool eh_only)10950 push_cleanup (tree decl, tree cleanup, bool eh_only)
10951 {
10952 enum tree_code code;
10953 tree stmt, list;
10954 bool stmt_expr;
10955
10956 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10957 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10958 add_stmt (stmt);
10959 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10960 list = push_stmt_list ();
10961 TREE_OPERAND (stmt, 0) = list;
10962 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10963 }
10964
10965 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10966 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10967
10968 static tree
build_vec_cmp(tree_code code,tree type,tree arg0,tree arg1)10969 build_vec_cmp (tree_code code, tree type,
10970 tree arg0, tree arg1)
10971 {
10972 tree zero_vec = build_zero_cst (type);
10973 tree minus_one_vec = build_minus_one_cst (type);
10974 tree cmp_type = build_same_sized_truth_vector_type (type);
10975 tree cmp = build2 (code, cmp_type, arg0, arg1);
10976 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10977 }
10978
10979 /* Build a binary-operation expression without default conversions.
10980 CODE is the kind of expression to build.
10981 LOCATION is the operator's location.
10982 This function differs from `build' in several ways:
10983 the data type of the result is computed and recorded in it,
10984 warnings are generated if arg data types are invalid,
10985 special handling for addition and subtraction of pointers is known,
10986 and some optimization is done (operations on narrow ints
10987 are done in the narrower type when that gives the same result).
10988 Constant folding is also done before the result is returned.
10989
10990 Note that the operands will never have enumeral types, or function
10991 or array types, because either they will have the default conversions
10992 performed or they have both just been converted to some other type in which
10993 the arithmetic is to be done. */
10994
10995 tree
build_binary_op(location_t location,enum tree_code code,tree orig_op0,tree orig_op1,bool convert_p)10996 build_binary_op (location_t location, enum tree_code code,
10997 tree orig_op0, tree orig_op1, bool convert_p)
10998 {
10999 tree type0, type1, orig_type0, orig_type1;
11000 tree eptype;
11001 enum tree_code code0, code1;
11002 tree op0, op1;
11003 tree ret = error_mark_node;
11004 const char *invalid_op_diag;
11005 bool op0_int_operands, op1_int_operands;
11006 bool int_const, int_const_or_overflow, int_operands;
11007
11008 /* Expression code to give to the expression when it is built.
11009 Normally this is CODE, which is what the caller asked for,
11010 but in some special cases we change it. */
11011 enum tree_code resultcode = code;
11012
11013 /* Data type in which the computation is to be performed.
11014 In the simplest cases this is the common type of the arguments. */
11015 tree result_type = NULL;
11016
11017 /* When the computation is in excess precision, the type of the
11018 final EXCESS_PRECISION_EXPR. */
11019 tree semantic_result_type = NULL;
11020
11021 /* Nonzero means operands have already been type-converted
11022 in whatever way is necessary.
11023 Zero means they need to be converted to RESULT_TYPE. */
11024 int converted = 0;
11025
11026 /* Nonzero means create the expression with this type, rather than
11027 RESULT_TYPE. */
11028 tree build_type = NULL_TREE;
11029
11030 /* Nonzero means after finally constructing the expression
11031 convert it to this type. */
11032 tree final_type = NULL_TREE;
11033
11034 /* Nonzero if this is an operation like MIN or MAX which can
11035 safely be computed in short if both args are promoted shorts.
11036 Also implies COMMON.
11037 -1 indicates a bitwise operation; this makes a difference
11038 in the exact conditions for when it is safe to do the operation
11039 in a narrower mode. */
11040 int shorten = 0;
11041
11042 /* Nonzero if this is a comparison operation;
11043 if both args are promoted shorts, compare the original shorts.
11044 Also implies COMMON. */
11045 int short_compare = 0;
11046
11047 /* Nonzero if this is a right-shift operation, which can be computed on the
11048 original short and then promoted if the operand is a promoted short. */
11049 int short_shift = 0;
11050
11051 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11052 int common = 0;
11053
11054 /* True means types are compatible as far as ObjC is concerned. */
11055 bool objc_ok;
11056
11057 /* True means this is an arithmetic operation that may need excess
11058 precision. */
11059 bool may_need_excess_precision;
11060
11061 /* True means this is a boolean operation that converts both its
11062 operands to truth-values. */
11063 bool boolean_op = false;
11064
11065 /* Remember whether we're doing / or %. */
11066 bool doing_div_or_mod = false;
11067
11068 /* Remember whether we're doing << or >>. */
11069 bool doing_shift = false;
11070
11071 /* Tree holding instrumentation expression. */
11072 tree instrument_expr = NULL;
11073
11074 if (location == UNKNOWN_LOCATION)
11075 location = input_location;
11076
11077 op0 = orig_op0;
11078 op1 = orig_op1;
11079
11080 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11081 if (op0_int_operands)
11082 op0 = remove_c_maybe_const_expr (op0);
11083 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11084 if (op1_int_operands)
11085 op1 = remove_c_maybe_const_expr (op1);
11086 int_operands = (op0_int_operands && op1_int_operands);
11087 if (int_operands)
11088 {
11089 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11090 && TREE_CODE (orig_op1) == INTEGER_CST);
11091 int_const = (int_const_or_overflow
11092 && !TREE_OVERFLOW (orig_op0)
11093 && !TREE_OVERFLOW (orig_op1));
11094 }
11095 else
11096 int_const = int_const_or_overflow = false;
11097
11098 /* Do not apply default conversion in mixed vector/scalar expression. */
11099 if (convert_p
11100 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11101 {
11102 op0 = default_conversion (op0);
11103 op1 = default_conversion (op1);
11104 }
11105
11106 orig_type0 = type0 = TREE_TYPE (op0);
11107
11108 orig_type1 = type1 = TREE_TYPE (op1);
11109
11110 /* The expression codes of the data types of the arguments tell us
11111 whether the arguments are integers, floating, pointers, etc. */
11112 code0 = TREE_CODE (type0);
11113 code1 = TREE_CODE (type1);
11114
11115 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11116 STRIP_TYPE_NOPS (op0);
11117 STRIP_TYPE_NOPS (op1);
11118
11119 /* If an error was already reported for one of the arguments,
11120 avoid reporting another error. */
11121
11122 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11123 return error_mark_node;
11124
11125 if (code0 == POINTER_TYPE
11126 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11127 return error_mark_node;
11128
11129 if (code1 == POINTER_TYPE
11130 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11131 return error_mark_node;
11132
11133 if ((invalid_op_diag
11134 = targetm.invalid_binary_op (code, type0, type1)))
11135 {
11136 error_at (location, invalid_op_diag);
11137 return error_mark_node;
11138 }
11139
11140 switch (code)
11141 {
11142 case PLUS_EXPR:
11143 case MINUS_EXPR:
11144 case MULT_EXPR:
11145 case TRUNC_DIV_EXPR:
11146 case CEIL_DIV_EXPR:
11147 case FLOOR_DIV_EXPR:
11148 case ROUND_DIV_EXPR:
11149 case EXACT_DIV_EXPR:
11150 may_need_excess_precision = true;
11151 break;
11152 default:
11153 may_need_excess_precision = false;
11154 break;
11155 }
11156 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11157 {
11158 op0 = TREE_OPERAND (op0, 0);
11159 type0 = TREE_TYPE (op0);
11160 }
11161 else if (may_need_excess_precision
11162 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11163 {
11164 type0 = eptype;
11165 op0 = convert (eptype, op0);
11166 }
11167 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11168 {
11169 op1 = TREE_OPERAND (op1, 0);
11170 type1 = TREE_TYPE (op1);
11171 }
11172 else if (may_need_excess_precision
11173 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11174 {
11175 type1 = eptype;
11176 op1 = convert (eptype, op1);
11177 }
11178
11179 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11180
11181 /* In case when one of the operands of the binary operation is
11182 a vector and another is a scalar -- convert scalar to vector. */
11183 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
11184 {
11185 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11186 true);
11187
11188 switch (convert_flag)
11189 {
11190 case stv_error:
11191 return error_mark_node;
11192 case stv_firstarg:
11193 {
11194 bool maybe_const = true;
11195 tree sc;
11196 sc = c_fully_fold (op0, false, &maybe_const);
11197 sc = save_expr (sc);
11198 sc = convert (TREE_TYPE (type1), sc);
11199 op0 = build_vector_from_val (type1, sc);
11200 if (!maybe_const)
11201 op0 = c_wrap_maybe_const (op0, true);
11202 orig_type0 = type0 = TREE_TYPE (op0);
11203 code0 = TREE_CODE (type0);
11204 converted = 1;
11205 break;
11206 }
11207 case stv_secondarg:
11208 {
11209 bool maybe_const = true;
11210 tree sc;
11211 sc = c_fully_fold (op1, false, &maybe_const);
11212 sc = save_expr (sc);
11213 sc = convert (TREE_TYPE (type0), sc);
11214 op1 = build_vector_from_val (type0, sc);
11215 if (!maybe_const)
11216 op1 = c_wrap_maybe_const (op1, true);
11217 orig_type1 = type1 = TREE_TYPE (op1);
11218 code1 = TREE_CODE (type1);
11219 converted = 1;
11220 break;
11221 }
11222 default:
11223 break;
11224 }
11225 }
11226
11227 switch (code)
11228 {
11229 case PLUS_EXPR:
11230 /* Handle the pointer + int case. */
11231 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11232 {
11233 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11234 goto return_build_binary_op;
11235 }
11236 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11237 {
11238 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11239 goto return_build_binary_op;
11240 }
11241 else
11242 common = 1;
11243 break;
11244
11245 case MINUS_EXPR:
11246 /* Subtraction of two similar pointers.
11247 We must subtract them as integers, then divide by object size. */
11248 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11249 && comp_target_types (location, type0, type1))
11250 {
11251 ret = pointer_diff (location, op0, op1, &instrument_expr);
11252 goto return_build_binary_op;
11253 }
11254 /* Handle pointer minus int. Just like pointer plus int. */
11255 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11256 {
11257 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11258 goto return_build_binary_op;
11259 }
11260 else
11261 common = 1;
11262 break;
11263
11264 case MULT_EXPR:
11265 common = 1;
11266 break;
11267
11268 case TRUNC_DIV_EXPR:
11269 case CEIL_DIV_EXPR:
11270 case FLOOR_DIV_EXPR:
11271 case ROUND_DIV_EXPR:
11272 case EXACT_DIV_EXPR:
11273 doing_div_or_mod = true;
11274 warn_for_div_by_zero (location, op1);
11275
11276 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11277 || code0 == FIXED_POINT_TYPE
11278 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11279 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11280 || code1 == FIXED_POINT_TYPE
11281 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
11282 {
11283 enum tree_code tcode0 = code0, tcode1 = code1;
11284
11285 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11286 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11287 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11288 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11289
11290 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11291 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11292 resultcode = RDIV_EXPR;
11293 else
11294 /* Although it would be tempting to shorten always here, that
11295 loses on some targets, since the modulo instruction is
11296 undefined if the quotient can't be represented in the
11297 computation mode. We shorten only if unsigned or if
11298 dividing by something we know != -1. */
11299 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11300 || (TREE_CODE (op1) == INTEGER_CST
11301 && !integer_all_onesp (op1)));
11302 common = 1;
11303 }
11304 break;
11305
11306 case BIT_AND_EXPR:
11307 case BIT_IOR_EXPR:
11308 case BIT_XOR_EXPR:
11309 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11310 shorten = -1;
11311 /* Allow vector types which are not floating point types. */
11312 else if (code0 == VECTOR_TYPE
11313 && code1 == VECTOR_TYPE
11314 && !VECTOR_FLOAT_TYPE_P (type0)
11315 && !VECTOR_FLOAT_TYPE_P (type1))
11316 common = 1;
11317 break;
11318
11319 case TRUNC_MOD_EXPR:
11320 case FLOOR_MOD_EXPR:
11321 doing_div_or_mod = true;
11322 warn_for_div_by_zero (location, op1);
11323
11324 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11325 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11326 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11327 common = 1;
11328 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11329 {
11330 /* Although it would be tempting to shorten always here, that loses
11331 on some targets, since the modulo instruction is undefined if the
11332 quotient can't be represented in the computation mode. We shorten
11333 only if unsigned or if dividing by something we know != -1. */
11334 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11335 || (TREE_CODE (op1) == INTEGER_CST
11336 && !integer_all_onesp (op1)));
11337 common = 1;
11338 }
11339 break;
11340
11341 case TRUTH_ANDIF_EXPR:
11342 case TRUTH_ORIF_EXPR:
11343 case TRUTH_AND_EXPR:
11344 case TRUTH_OR_EXPR:
11345 case TRUTH_XOR_EXPR:
11346 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11347 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11348 || code0 == FIXED_POINT_TYPE)
11349 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11350 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11351 || code1 == FIXED_POINT_TYPE))
11352 {
11353 /* Result of these operations is always an int,
11354 but that does not mean the operands should be
11355 converted to ints! */
11356 result_type = integer_type_node;
11357 if (op0_int_operands)
11358 {
11359 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11360 op0 = remove_c_maybe_const_expr (op0);
11361 }
11362 else
11363 op0 = c_objc_common_truthvalue_conversion (location, op0);
11364 if (op1_int_operands)
11365 {
11366 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11367 op1 = remove_c_maybe_const_expr (op1);
11368 }
11369 else
11370 op1 = c_objc_common_truthvalue_conversion (location, op1);
11371 converted = 1;
11372 boolean_op = true;
11373 }
11374 if (code == TRUTH_ANDIF_EXPR)
11375 {
11376 int_const_or_overflow = (int_operands
11377 && TREE_CODE (orig_op0) == INTEGER_CST
11378 && (op0 == truthvalue_false_node
11379 || TREE_CODE (orig_op1) == INTEGER_CST));
11380 int_const = (int_const_or_overflow
11381 && !TREE_OVERFLOW (orig_op0)
11382 && (op0 == truthvalue_false_node
11383 || !TREE_OVERFLOW (orig_op1)));
11384 }
11385 else if (code == TRUTH_ORIF_EXPR)
11386 {
11387 int_const_or_overflow = (int_operands
11388 && TREE_CODE (orig_op0) == INTEGER_CST
11389 && (op0 == truthvalue_true_node
11390 || TREE_CODE (orig_op1) == INTEGER_CST));
11391 int_const = (int_const_or_overflow
11392 && !TREE_OVERFLOW (orig_op0)
11393 && (op0 == truthvalue_true_node
11394 || !TREE_OVERFLOW (orig_op1)));
11395 }
11396 break;
11397
11398 /* Shift operations: result has same type as first operand;
11399 always convert second operand to int.
11400 Also set SHORT_SHIFT if shifting rightward. */
11401
11402 case RSHIFT_EXPR:
11403 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11404 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11405 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11406 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11407 TYPE_VECTOR_SUBPARTS (type1)))
11408 {
11409 result_type = type0;
11410 converted = 1;
11411 }
11412 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11413 || (code0 == VECTOR_TYPE
11414 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11415 && code1 == INTEGER_TYPE)
11416 {
11417 doing_shift = true;
11418 if (TREE_CODE (op1) == INTEGER_CST)
11419 {
11420 if (tree_int_cst_sgn (op1) < 0)
11421 {
11422 int_const = false;
11423 if (c_inhibit_evaluation_warnings == 0)
11424 warning_at (location, OPT_Wshift_count_negative,
11425 "right shift count is negative");
11426 }
11427 else if (code0 == VECTOR_TYPE)
11428 {
11429 if (compare_tree_int (op1,
11430 TYPE_PRECISION (TREE_TYPE (type0)))
11431 >= 0)
11432 {
11433 int_const = false;
11434 if (c_inhibit_evaluation_warnings == 0)
11435 warning_at (location, OPT_Wshift_count_overflow,
11436 "right shift count >= width of vector element");
11437 }
11438 }
11439 else
11440 {
11441 if (!integer_zerop (op1))
11442 short_shift = 1;
11443
11444 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11445 {
11446 int_const = false;
11447 if (c_inhibit_evaluation_warnings == 0)
11448 warning_at (location, OPT_Wshift_count_overflow,
11449 "right shift count >= width of type");
11450 }
11451 }
11452 }
11453
11454 /* Use the type of the value to be shifted. */
11455 result_type = type0;
11456 /* Avoid converting op1 to result_type later. */
11457 converted = 1;
11458 }
11459 break;
11460
11461 case LSHIFT_EXPR:
11462 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11463 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11464 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11465 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11466 TYPE_VECTOR_SUBPARTS (type1)))
11467 {
11468 result_type = type0;
11469 converted = 1;
11470 }
11471 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11472 || (code0 == VECTOR_TYPE
11473 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11474 && code1 == INTEGER_TYPE)
11475 {
11476 doing_shift = true;
11477 if (TREE_CODE (op0) == INTEGER_CST
11478 && tree_int_cst_sgn (op0) < 0)
11479 {
11480 /* Don't reject a left shift of a negative value in a context
11481 where a constant expression is needed in C90. */
11482 if (flag_isoc99)
11483 int_const = false;
11484 if (c_inhibit_evaluation_warnings == 0)
11485 warning_at (location, OPT_Wshift_negative_value,
11486 "left shift of negative value");
11487 }
11488 if (TREE_CODE (op1) == INTEGER_CST)
11489 {
11490 if (tree_int_cst_sgn (op1) < 0)
11491 {
11492 int_const = false;
11493 if (c_inhibit_evaluation_warnings == 0)
11494 warning_at (location, OPT_Wshift_count_negative,
11495 "left shift count is negative");
11496 }
11497 else if (code0 == VECTOR_TYPE)
11498 {
11499 if (compare_tree_int (op1,
11500 TYPE_PRECISION (TREE_TYPE (type0)))
11501 >= 0)
11502 {
11503 int_const = false;
11504 if (c_inhibit_evaluation_warnings == 0)
11505 warning_at (location, OPT_Wshift_count_overflow,
11506 "left shift count >= width of vector element");
11507 }
11508 }
11509 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11510 {
11511 int_const = false;
11512 if (c_inhibit_evaluation_warnings == 0)
11513 warning_at (location, OPT_Wshift_count_overflow,
11514 "left shift count >= width of type");
11515 }
11516 else if (TREE_CODE (op0) == INTEGER_CST
11517 && maybe_warn_shift_overflow (location, op0, op1)
11518 && flag_isoc99)
11519 int_const = false;
11520 }
11521
11522 /* Use the type of the value to be shifted. */
11523 result_type = type0;
11524 /* Avoid converting op1 to result_type later. */
11525 converted = 1;
11526 }
11527 break;
11528
11529 case EQ_EXPR:
11530 case NE_EXPR:
11531 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11532 {
11533 tree intt;
11534 if (!vector_types_compatible_elements_p (type0, type1))
11535 {
11536 error_at (location, "comparing vectors with different "
11537 "element types");
11538 return error_mark_node;
11539 }
11540
11541 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
11542 TYPE_VECTOR_SUBPARTS (type1)))
11543 {
11544 error_at (location, "comparing vectors with different "
11545 "number of elements");
11546 return error_mark_node;
11547 }
11548
11549 /* It's not precisely specified how the usual arithmetic
11550 conversions apply to the vector types. Here, we use
11551 the unsigned type if one of the operands is signed and
11552 the other one is unsigned. */
11553 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11554 {
11555 if (!TYPE_UNSIGNED (type0))
11556 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11557 else
11558 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11559 warning_at (location, OPT_Wsign_compare, "comparison between "
11560 "types %qT and %qT", type0, type1);
11561 }
11562
11563 /* Always construct signed integer vector type. */
11564 intt = c_common_type_for_size (GET_MODE_BITSIZE
11565 (SCALAR_TYPE_MODE
11566 (TREE_TYPE (type0))), 0);
11567 if (!intt)
11568 {
11569 error_at (location, "could not find an integer type "
11570 "of the same size as %qT",
11571 TREE_TYPE (type0));
11572 return error_mark_node;
11573 }
11574 result_type = build_opaque_vector_type (intt,
11575 TYPE_VECTOR_SUBPARTS (type0));
11576 converted = 1;
11577 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11578 goto return_build_binary_op;
11579 }
11580 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11581 warning_at (location,
11582 OPT_Wfloat_equal,
11583 "comparing floating point with == or != is unsafe");
11584 /* Result of comparison is always int,
11585 but don't convert the args to int! */
11586 build_type = integer_type_node;
11587 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11588 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11589 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11590 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11591 short_compare = 1;
11592 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11593 {
11594 if (TREE_CODE (op0) == ADDR_EXPR
11595 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11596 && !from_macro_expansion_at (location))
11597 {
11598 if (code == EQ_EXPR)
11599 warning_at (location,
11600 OPT_Waddress,
11601 "the comparison will always evaluate as %<false%> "
11602 "for the address of %qD will never be NULL",
11603 TREE_OPERAND (op0, 0));
11604 else
11605 warning_at (location,
11606 OPT_Waddress,
11607 "the comparison will always evaluate as %<true%> "
11608 "for the address of %qD will never be NULL",
11609 TREE_OPERAND (op0, 0));
11610 }
11611 result_type = type0;
11612 }
11613 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11614 {
11615 if (TREE_CODE (op1) == ADDR_EXPR
11616 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11617 && !from_macro_expansion_at (location))
11618 {
11619 if (code == EQ_EXPR)
11620 warning_at (location,
11621 OPT_Waddress,
11622 "the comparison will always evaluate as %<false%> "
11623 "for the address of %qD will never be NULL",
11624 TREE_OPERAND (op1, 0));
11625 else
11626 warning_at (location,
11627 OPT_Waddress,
11628 "the comparison will always evaluate as %<true%> "
11629 "for the address of %qD will never be NULL",
11630 TREE_OPERAND (op1, 0));
11631 }
11632 result_type = type1;
11633 }
11634 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11635 {
11636 tree tt0 = TREE_TYPE (type0);
11637 tree tt1 = TREE_TYPE (type1);
11638 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11639 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11640 addr_space_t as_common = ADDR_SPACE_GENERIC;
11641
11642 /* Anything compares with void *. void * compares with anything.
11643 Otherwise, the targets must be compatible
11644 and both must be object or both incomplete. */
11645 if (comp_target_types (location, type0, type1))
11646 result_type = common_pointer_type (type0, type1);
11647 else if (!addr_space_superset (as0, as1, &as_common))
11648 {
11649 error_at (location, "comparison of pointers to "
11650 "disjoint address spaces");
11651 return error_mark_node;
11652 }
11653 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11654 {
11655 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11656 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11657 "comparison of %<void *%> with function pointer");
11658 }
11659 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11660 {
11661 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11662 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11663 "comparison of %<void *%> with function pointer");
11664 }
11665 else
11666 /* Avoid warning about the volatile ObjC EH puts on decls. */
11667 if (!objc_ok)
11668 pedwarn (location, 0,
11669 "comparison of distinct pointer types lacks a cast");
11670
11671 if (result_type == NULL_TREE)
11672 {
11673 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11674 result_type = build_pointer_type
11675 (build_qualified_type (void_type_node, qual));
11676 }
11677 }
11678 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11679 {
11680 result_type = type0;
11681 pedwarn (location, 0, "comparison between pointer and integer");
11682 }
11683 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11684 {
11685 result_type = type1;
11686 pedwarn (location, 0, "comparison between pointer and integer");
11687 }
11688 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11689 || truth_value_p (TREE_CODE (orig_op0)))
11690 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11691 || truth_value_p (TREE_CODE (orig_op1))))
11692 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11693 break;
11694
11695 case LE_EXPR:
11696 case GE_EXPR:
11697 case LT_EXPR:
11698 case GT_EXPR:
11699 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11700 {
11701 tree intt;
11702 if (!vector_types_compatible_elements_p (type0, type1))
11703 {
11704 error_at (location, "comparing vectors with different "
11705 "element types");
11706 return error_mark_node;
11707 }
11708
11709 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
11710 TYPE_VECTOR_SUBPARTS (type1)))
11711 {
11712 error_at (location, "comparing vectors with different "
11713 "number of elements");
11714 return error_mark_node;
11715 }
11716
11717 /* It's not precisely specified how the usual arithmetic
11718 conversions apply to the vector types. Here, we use
11719 the unsigned type if one of the operands is signed and
11720 the other one is unsigned. */
11721 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11722 {
11723 if (!TYPE_UNSIGNED (type0))
11724 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11725 else
11726 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11727 warning_at (location, OPT_Wsign_compare, "comparison between "
11728 "types %qT and %qT", type0, type1);
11729 }
11730
11731 /* Always construct signed integer vector type. */
11732 intt = c_common_type_for_size (GET_MODE_BITSIZE
11733 (SCALAR_TYPE_MODE
11734 (TREE_TYPE (type0))), 0);
11735 if (!intt)
11736 {
11737 error_at (location, "could not find an integer type "
11738 "of the same size as %qT",
11739 TREE_TYPE (type0));
11740 return error_mark_node;
11741 }
11742 result_type = build_opaque_vector_type (intt,
11743 TYPE_VECTOR_SUBPARTS (type0));
11744 converted = 1;
11745 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11746 goto return_build_binary_op;
11747 }
11748 build_type = integer_type_node;
11749 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11750 || code0 == FIXED_POINT_TYPE)
11751 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11752 || code1 == FIXED_POINT_TYPE))
11753 short_compare = 1;
11754 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11755 {
11756 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11757 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11758 addr_space_t as_common;
11759
11760 if (comp_target_types (location, type0, type1))
11761 {
11762 result_type = common_pointer_type (type0, type1);
11763 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11764 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11765 pedwarn (location, 0,
11766 "comparison of complete and incomplete pointers");
11767 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11768 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11769 "ordered comparisons of pointers to functions");
11770 else if (null_pointer_constant_p (orig_op0)
11771 || null_pointer_constant_p (orig_op1))
11772 warning_at (location, OPT_Wextra,
11773 "ordered comparison of pointer with null pointer");
11774
11775 }
11776 else if (!addr_space_superset (as0, as1, &as_common))
11777 {
11778 error_at (location, "comparison of pointers to "
11779 "disjoint address spaces");
11780 return error_mark_node;
11781 }
11782 else
11783 {
11784 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11785 result_type = build_pointer_type
11786 (build_qualified_type (void_type_node, qual));
11787 pedwarn (location, 0,
11788 "comparison of distinct pointer types lacks a cast");
11789 }
11790 }
11791 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11792 {
11793 result_type = type0;
11794 if (pedantic)
11795 pedwarn (location, OPT_Wpedantic,
11796 "ordered comparison of pointer with integer zero");
11797 else if (extra_warnings)
11798 warning_at (location, OPT_Wextra,
11799 "ordered comparison of pointer with integer zero");
11800 }
11801 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11802 {
11803 result_type = type1;
11804 if (pedantic)
11805 pedwarn (location, OPT_Wpedantic,
11806 "ordered comparison of pointer with integer zero");
11807 else if (extra_warnings)
11808 warning_at (location, OPT_Wextra,
11809 "ordered comparison of pointer with integer zero");
11810 }
11811 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11812 {
11813 result_type = type0;
11814 pedwarn (location, 0, "comparison between pointer and integer");
11815 }
11816 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11817 {
11818 result_type = type1;
11819 pedwarn (location, 0, "comparison between pointer and integer");
11820 }
11821
11822 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
11823 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
11824 {
11825 op0 = save_expr (op0);
11826 op1 = save_expr (op1);
11827
11828 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
11829 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
11830 }
11831
11832 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11833 || truth_value_p (TREE_CODE (orig_op0)))
11834 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11835 || truth_value_p (TREE_CODE (orig_op1))))
11836 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11837 break;
11838
11839 default:
11840 gcc_unreachable ();
11841 }
11842
11843 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11844 return error_mark_node;
11845
11846 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11847 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11848 || !vector_types_compatible_elements_p (type0, type1)))
11849 {
11850 gcc_rich_location richloc (location);
11851 richloc.maybe_add_expr (orig_op0);
11852 richloc.maybe_add_expr (orig_op1);
11853 binary_op_error (&richloc, code, type0, type1);
11854 return error_mark_node;
11855 }
11856
11857 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11858 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11859 &&
11860 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11861 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11862 {
11863 bool first_complex = (code0 == COMPLEX_TYPE);
11864 bool second_complex = (code1 == COMPLEX_TYPE);
11865 int none_complex = (!first_complex && !second_complex);
11866
11867 if (shorten || common || short_compare)
11868 {
11869 result_type = c_common_type (type0, type1);
11870 do_warn_double_promotion (result_type, type0, type1,
11871 "implicit conversion from %qT to %qT "
11872 "to match other operand of binary "
11873 "expression",
11874 location);
11875 if (result_type == error_mark_node)
11876 return error_mark_node;
11877 }
11878
11879 if (first_complex != second_complex
11880 && (code == PLUS_EXPR
11881 || code == MINUS_EXPR
11882 || code == MULT_EXPR
11883 || (code == TRUNC_DIV_EXPR && first_complex))
11884 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11885 && flag_signed_zeros)
11886 {
11887 /* An operation on mixed real/complex operands must be
11888 handled specially, but the language-independent code can
11889 more easily optimize the plain complex arithmetic if
11890 -fno-signed-zeros. */
11891 tree real_type = TREE_TYPE (result_type);
11892 tree real, imag;
11893 if (type0 != orig_type0 || type1 != orig_type1)
11894 {
11895 gcc_assert (may_need_excess_precision && common);
11896 semantic_result_type = c_common_type (orig_type0, orig_type1);
11897 }
11898 if (first_complex)
11899 {
11900 if (TREE_TYPE (op0) != result_type)
11901 op0 = convert_and_check (location, result_type, op0);
11902 if (TREE_TYPE (op1) != real_type)
11903 op1 = convert_and_check (location, real_type, op1);
11904 }
11905 else
11906 {
11907 if (TREE_TYPE (op0) != real_type)
11908 op0 = convert_and_check (location, real_type, op0);
11909 if (TREE_TYPE (op1) != result_type)
11910 op1 = convert_and_check (location, result_type, op1);
11911 }
11912 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11913 return error_mark_node;
11914 if (first_complex)
11915 {
11916 op0 = save_expr (op0);
11917 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11918 op0, true);
11919 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11920 op0, true);
11921 switch (code)
11922 {
11923 case MULT_EXPR:
11924 case TRUNC_DIV_EXPR:
11925 op1 = save_expr (op1);
11926 imag = build2 (resultcode, real_type, imag, op1);
11927 /* Fall through. */
11928 case PLUS_EXPR:
11929 case MINUS_EXPR:
11930 real = build2 (resultcode, real_type, real, op1);
11931 break;
11932 default:
11933 gcc_unreachable();
11934 }
11935 }
11936 else
11937 {
11938 op1 = save_expr (op1);
11939 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11940 op1, true);
11941 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11942 op1, true);
11943 switch (code)
11944 {
11945 case MULT_EXPR:
11946 op0 = save_expr (op0);
11947 imag = build2 (resultcode, real_type, op0, imag);
11948 /* Fall through. */
11949 case PLUS_EXPR:
11950 real = build2 (resultcode, real_type, op0, real);
11951 break;
11952 case MINUS_EXPR:
11953 real = build2 (resultcode, real_type, op0, real);
11954 imag = build1 (NEGATE_EXPR, real_type, imag);
11955 break;
11956 default:
11957 gcc_unreachable();
11958 }
11959 }
11960 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11961 goto return_build_binary_op;
11962 }
11963
11964 /* For certain operations (which identify themselves by shorten != 0)
11965 if both args were extended from the same smaller type,
11966 do the arithmetic in that type and then extend.
11967
11968 shorten !=0 and !=1 indicates a bitwise operation.
11969 For them, this optimization is safe only if
11970 both args are zero-extended or both are sign-extended.
11971 Otherwise, we might change the result.
11972 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11973 but calculated in (unsigned short) it would be (unsigned short)-1. */
11974
11975 if (shorten && none_complex)
11976 {
11977 final_type = result_type;
11978 result_type = shorten_binary_op (result_type, op0, op1,
11979 shorten == -1);
11980 }
11981
11982 /* Shifts can be shortened if shifting right. */
11983
11984 if (short_shift)
11985 {
11986 int unsigned_arg;
11987 tree arg0 = get_narrower (op0, &unsigned_arg);
11988
11989 final_type = result_type;
11990
11991 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11992 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11993
11994 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11995 && tree_int_cst_sgn (op1) > 0
11996 /* We can shorten only if the shift count is less than the
11997 number of bits in the smaller type size. */
11998 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11999 /* We cannot drop an unsigned shift after sign-extension. */
12000 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12001 {
12002 /* Do an unsigned shift if the operand was zero-extended. */
12003 result_type
12004 = c_common_signed_or_unsigned_type (unsigned_arg,
12005 TREE_TYPE (arg0));
12006 /* Convert value-to-be-shifted to that type. */
12007 if (TREE_TYPE (op0) != result_type)
12008 op0 = convert (result_type, op0);
12009 converted = 1;
12010 }
12011 }
12012
12013 /* Comparison operations are shortened too but differently.
12014 They identify themselves by setting short_compare = 1. */
12015
12016 if (short_compare)
12017 {
12018 /* Don't write &op0, etc., because that would prevent op0
12019 from being kept in a register.
12020 Instead, make copies of the our local variables and
12021 pass the copies by reference, then copy them back afterward. */
12022 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12023 enum tree_code xresultcode = resultcode;
12024 tree val
12025 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12026 &xresultcode);
12027
12028 if (val != NULL_TREE)
12029 {
12030 ret = val;
12031 goto return_build_binary_op;
12032 }
12033
12034 op0 = xop0, op1 = xop1;
12035 converted = 1;
12036 resultcode = xresultcode;
12037
12038 if (c_inhibit_evaluation_warnings == 0)
12039 {
12040 bool op0_maybe_const = true;
12041 bool op1_maybe_const = true;
12042 tree orig_op0_folded, orig_op1_folded;
12043
12044 if (in_late_binary_op)
12045 {
12046 orig_op0_folded = orig_op0;
12047 orig_op1_folded = orig_op1;
12048 }
12049 else
12050 {
12051 /* Fold for the sake of possible warnings, as in
12052 build_conditional_expr. This requires the
12053 "original" values to be folded, not just op0 and
12054 op1. */
12055 c_inhibit_evaluation_warnings++;
12056 op0 = c_fully_fold (op0, require_constant_value,
12057 &op0_maybe_const);
12058 op1 = c_fully_fold (op1, require_constant_value,
12059 &op1_maybe_const);
12060 c_inhibit_evaluation_warnings--;
12061 orig_op0_folded = c_fully_fold (orig_op0,
12062 require_constant_value,
12063 NULL);
12064 orig_op1_folded = c_fully_fold (orig_op1,
12065 require_constant_value,
12066 NULL);
12067 }
12068
12069 if (warn_sign_compare)
12070 warn_for_sign_compare (location, orig_op0_folded,
12071 orig_op1_folded, op0, op1,
12072 result_type, resultcode);
12073 if (!in_late_binary_op && !int_operands)
12074 {
12075 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12076 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12077 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12078 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12079 }
12080 }
12081 }
12082 }
12083
12084 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12085 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12086 Then the expression will be built.
12087 It will be given type FINAL_TYPE if that is nonzero;
12088 otherwise, it will be given type RESULT_TYPE. */
12089
12090 if (!result_type)
12091 {
12092 gcc_rich_location richloc (location);
12093 richloc.maybe_add_expr (orig_op0);
12094 richloc.maybe_add_expr (orig_op1);
12095 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12096 return error_mark_node;
12097 }
12098
12099 if (build_type == NULL_TREE)
12100 {
12101 build_type = result_type;
12102 if ((type0 != orig_type0 || type1 != orig_type1)
12103 && !boolean_op)
12104 {
12105 gcc_assert (may_need_excess_precision && common);
12106 semantic_result_type = c_common_type (orig_type0, orig_type1);
12107 }
12108 }
12109
12110 if (!converted)
12111 {
12112 op0 = ep_convert_and_check (location, result_type, op0,
12113 semantic_result_type);
12114 op1 = ep_convert_and_check (location, result_type, op1,
12115 semantic_result_type);
12116
12117 /* This can happen if one operand has a vector type, and the other
12118 has a different type. */
12119 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12120 return error_mark_node;
12121 }
12122
12123 if (sanitize_flags_p ((SANITIZE_SHIFT
12124 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
12125 && current_function_decl != NULL_TREE
12126 && (doing_div_or_mod || doing_shift)
12127 && !require_constant_value)
12128 {
12129 /* OP0 and/or OP1 might have side-effects. */
12130 op0 = save_expr (op0);
12131 op1 = save_expr (op1);
12132 op0 = c_fully_fold (op0, false, NULL);
12133 op1 = c_fully_fold (op1, false, NULL);
12134 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12135 | SANITIZE_FLOAT_DIVIDE))))
12136 instrument_expr = ubsan_instrument_division (location, op0, op1);
12137 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12138 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12139 }
12140
12141 /* Treat expressions in initializers specially as they can't trap. */
12142 if (int_const_or_overflow)
12143 ret = (require_constant_value
12144 ? fold_build2_initializer_loc (location, resultcode, build_type,
12145 op0, op1)
12146 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12147 else
12148 ret = build2 (resultcode, build_type, op0, op1);
12149 if (final_type != NULL_TREE)
12150 ret = convert (final_type, ret);
12151
12152 return_build_binary_op:
12153 gcc_assert (ret != error_mark_node);
12154 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12155 ret = (int_operands
12156 ? note_integer_operands (ret)
12157 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12158 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12159 && !in_late_binary_op)
12160 ret = note_integer_operands (ret);
12161 protected_set_expr_location (ret, location);
12162
12163 if (instrument_expr != NULL)
12164 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12165 instrument_expr, ret);
12166
12167 if (semantic_result_type)
12168 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12169 semantic_result_type, ret);
12170
12171 return ret;
12172 }
12173
12174
12175 /* Convert EXPR to be a truth-value, validating its type for this
12176 purpose. LOCATION is the source location for the expression. */
12177
12178 tree
c_objc_common_truthvalue_conversion(location_t location,tree expr)12179 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12180 {
12181 bool int_const, int_operands;
12182
12183 switch (TREE_CODE (TREE_TYPE (expr)))
12184 {
12185 case ARRAY_TYPE:
12186 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12187 return error_mark_node;
12188
12189 case RECORD_TYPE:
12190 error_at (location, "used struct type value where scalar is required");
12191 return error_mark_node;
12192
12193 case UNION_TYPE:
12194 error_at (location, "used union type value where scalar is required");
12195 return error_mark_node;
12196
12197 case VOID_TYPE:
12198 error_at (location, "void value not ignored as it ought to be");
12199 return error_mark_node;
12200
12201 case POINTER_TYPE:
12202 if (reject_gcc_builtin (expr))
12203 return error_mark_node;
12204 break;
12205
12206 case FUNCTION_TYPE:
12207 gcc_unreachable ();
12208
12209 case VECTOR_TYPE:
12210 error_at (location, "used vector type where scalar is required");
12211 return error_mark_node;
12212
12213 default:
12214 break;
12215 }
12216
12217 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12218 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12219 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12220 {
12221 expr = remove_c_maybe_const_expr (expr);
12222 expr = build2 (NE_EXPR, integer_type_node, expr,
12223 convert (TREE_TYPE (expr), integer_zero_node));
12224 expr = note_integer_operands (expr);
12225 }
12226 else
12227 /* ??? Should we also give an error for vectors rather than leaving
12228 those to give errors later? */
12229 expr = c_common_truthvalue_conversion (location, expr);
12230
12231 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12232 {
12233 if (TREE_OVERFLOW (expr))
12234 return expr;
12235 else
12236 return note_integer_operands (expr);
12237 }
12238 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12239 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12240 return expr;
12241 }
12242
12243
12244 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12245 required. */
12246
12247 tree
c_expr_to_decl(tree expr,bool * tc ATTRIBUTE_UNUSED,bool * se)12248 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12249 {
12250 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12251 {
12252 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12253 /* Executing a compound literal inside a function reinitializes
12254 it. */
12255 if (!TREE_STATIC (decl))
12256 *se = true;
12257 return decl;
12258 }
12259 else
12260 return expr;
12261 }
12262
12263 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12264 statement. LOC is the location of the construct. */
12265
12266 tree
c_finish_omp_construct(location_t loc,enum tree_code code,tree body,tree clauses)12267 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12268 tree clauses)
12269 {
12270 body = c_end_compound_stmt (loc, body, true);
12271
12272 tree stmt = make_node (code);
12273 TREE_TYPE (stmt) = void_type_node;
12274 OMP_BODY (stmt) = body;
12275 OMP_CLAUSES (stmt) = clauses;
12276 SET_EXPR_LOCATION (stmt, loc);
12277
12278 return add_stmt (stmt);
12279 }
12280
12281 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12282 statement. LOC is the location of the OACC_DATA. */
12283
12284 tree
c_finish_oacc_data(location_t loc,tree clauses,tree block)12285 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12286 {
12287 tree stmt;
12288
12289 block = c_end_compound_stmt (loc, block, true);
12290
12291 stmt = make_node (OACC_DATA);
12292 TREE_TYPE (stmt) = void_type_node;
12293 OACC_DATA_CLAUSES (stmt) = clauses;
12294 OACC_DATA_BODY (stmt) = block;
12295 SET_EXPR_LOCATION (stmt, loc);
12296
12297 return add_stmt (stmt);
12298 }
12299
12300 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12301 statement. LOC is the location of the OACC_HOST_DATA. */
12302
12303 tree
c_finish_oacc_host_data(location_t loc,tree clauses,tree block)12304 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12305 {
12306 tree stmt;
12307
12308 block = c_end_compound_stmt (loc, block, true);
12309
12310 stmt = make_node (OACC_HOST_DATA);
12311 TREE_TYPE (stmt) = void_type_node;
12312 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12313 OACC_HOST_DATA_BODY (stmt) = block;
12314 SET_EXPR_LOCATION (stmt, loc);
12315
12316 return add_stmt (stmt);
12317 }
12318
12319 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12320
12321 tree
c_begin_omp_parallel(void)12322 c_begin_omp_parallel (void)
12323 {
12324 tree block;
12325
12326 keep_next_level ();
12327 block = c_begin_compound_stmt (true);
12328
12329 return block;
12330 }
12331
12332 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12333 statement. LOC is the location of the OMP_PARALLEL. */
12334
12335 tree
c_finish_omp_parallel(location_t loc,tree clauses,tree block)12336 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12337 {
12338 tree stmt;
12339
12340 block = c_end_compound_stmt (loc, block, true);
12341
12342 stmt = make_node (OMP_PARALLEL);
12343 TREE_TYPE (stmt) = void_type_node;
12344 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12345 OMP_PARALLEL_BODY (stmt) = block;
12346 SET_EXPR_LOCATION (stmt, loc);
12347
12348 return add_stmt (stmt);
12349 }
12350
12351 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12352
12353 tree
c_begin_omp_task(void)12354 c_begin_omp_task (void)
12355 {
12356 tree block;
12357
12358 keep_next_level ();
12359 block = c_begin_compound_stmt (true);
12360
12361 return block;
12362 }
12363
12364 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12365 statement. LOC is the location of the #pragma. */
12366
12367 tree
c_finish_omp_task(location_t loc,tree clauses,tree block)12368 c_finish_omp_task (location_t loc, tree clauses, tree block)
12369 {
12370 tree stmt;
12371
12372 block = c_end_compound_stmt (loc, block, true);
12373
12374 stmt = make_node (OMP_TASK);
12375 TREE_TYPE (stmt) = void_type_node;
12376 OMP_TASK_CLAUSES (stmt) = clauses;
12377 OMP_TASK_BODY (stmt) = block;
12378 SET_EXPR_LOCATION (stmt, loc);
12379
12380 return add_stmt (stmt);
12381 }
12382
12383 /* Generate GOMP_cancel call for #pragma omp cancel. */
12384
12385 void
c_finish_omp_cancel(location_t loc,tree clauses)12386 c_finish_omp_cancel (location_t loc, tree clauses)
12387 {
12388 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12389 int mask = 0;
12390 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12391 mask = 1;
12392 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12393 mask = 2;
12394 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12395 mask = 4;
12396 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12397 mask = 8;
12398 else
12399 {
12400 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12401 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12402 "clauses");
12403 return;
12404 }
12405 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12406 if (ifc != NULL_TREE)
12407 {
12408 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12409 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12410 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12411 build_zero_cst (type));
12412 }
12413 else
12414 ifc = boolean_true_node;
12415 tree stmt = build_call_expr_loc (loc, fn, 2,
12416 build_int_cst (integer_type_node, mask),
12417 ifc);
12418 add_stmt (stmt);
12419 }
12420
12421 /* Generate GOMP_cancellation_point call for
12422 #pragma omp cancellation point. */
12423
12424 void
c_finish_omp_cancellation_point(location_t loc,tree clauses)12425 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12426 {
12427 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12428 int mask = 0;
12429 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12430 mask = 1;
12431 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12432 mask = 2;
12433 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12434 mask = 4;
12435 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12436 mask = 8;
12437 else
12438 {
12439 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12440 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12441 "clauses");
12442 return;
12443 }
12444 tree stmt = build_call_expr_loc (loc, fn, 1,
12445 build_int_cst (integer_type_node, mask));
12446 add_stmt (stmt);
12447 }
12448
12449 /* Helper function for handle_omp_array_sections. Called recursively
12450 to handle multiple array-section-subscripts. C is the clause,
12451 T current expression (initially OMP_CLAUSE_DECL), which is either
12452 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12453 expression if specified, TREE_VALUE length expression if specified,
12454 TREE_CHAIN is what it has been specified after, or some decl.
12455 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12456 set to true if any of the array-section-subscript could have length
12457 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12458 first array-section-subscript which is known not to have length
12459 of one. Given say:
12460 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12461 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12462 all are or may have length of 1, array-section-subscript [:2] is the
12463 first one known not to have length 1. For array-section-subscript
12464 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12465 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12466 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12467 case though, as some lengths could be zero. */
12468
12469 static tree
handle_omp_array_sections_1(tree c,tree t,vec<tree> & types,bool & maybe_zero_len,unsigned int & first_non_one,enum c_omp_region_type ort)12470 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12471 bool &maybe_zero_len, unsigned int &first_non_one,
12472 enum c_omp_region_type ort)
12473 {
12474 tree ret, low_bound, length, type;
12475 if (TREE_CODE (t) != TREE_LIST)
12476 {
12477 if (error_operand_p (t))
12478 return error_mark_node;
12479 ret = t;
12480 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12481 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12482 {
12483 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12484 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12485 return error_mark_node;
12486 }
12487 if (TREE_CODE (t) == COMPONENT_REF
12488 && ort == C_ORT_OMP
12489 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12490 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12491 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12492 {
12493 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12494 {
12495 error_at (OMP_CLAUSE_LOCATION (c),
12496 "bit-field %qE in %qs clause",
12497 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12498 return error_mark_node;
12499 }
12500 while (TREE_CODE (t) == COMPONENT_REF)
12501 {
12502 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12503 {
12504 error_at (OMP_CLAUSE_LOCATION (c),
12505 "%qE is a member of a union", t);
12506 return error_mark_node;
12507 }
12508 t = TREE_OPERAND (t, 0);
12509 }
12510 }
12511 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12512 {
12513 if (DECL_P (t))
12514 error_at (OMP_CLAUSE_LOCATION (c),
12515 "%qD is not a variable in %qs clause", t,
12516 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12517 else
12518 error_at (OMP_CLAUSE_LOCATION (c),
12519 "%qE is not a variable in %qs clause", t,
12520 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12521 return error_mark_node;
12522 }
12523 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12524 && TYPE_ATOMIC (TREE_TYPE (t)))
12525 {
12526 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12527 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12528 return error_mark_node;
12529 }
12530 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12531 && VAR_P (t)
12532 && DECL_THREAD_LOCAL_P (t))
12533 {
12534 error_at (OMP_CLAUSE_LOCATION (c),
12535 "%qD is threadprivate variable in %qs clause", t,
12536 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12537 return error_mark_node;
12538 }
12539 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12540 && TYPE_ATOMIC (TREE_TYPE (t))
12541 && POINTER_TYPE_P (TREE_TYPE (t)))
12542 {
12543 /* If the array section is pointer based and the pointer
12544 itself is _Atomic qualified, we need to atomically load
12545 the pointer. */
12546 c_expr expr;
12547 memset (&expr, 0, sizeof (expr));
12548 expr.value = ret;
12549 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12550 expr, false, false);
12551 ret = expr.value;
12552 }
12553 return ret;
12554 }
12555
12556 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12557 maybe_zero_len, first_non_one, ort);
12558 if (ret == error_mark_node || ret == NULL_TREE)
12559 return ret;
12560
12561 type = TREE_TYPE (ret);
12562 low_bound = TREE_PURPOSE (t);
12563 length = TREE_VALUE (t);
12564
12565 if (low_bound == error_mark_node || length == error_mark_node)
12566 return error_mark_node;
12567
12568 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12569 {
12570 error_at (OMP_CLAUSE_LOCATION (c),
12571 "low bound %qE of array section does not have integral type",
12572 low_bound);
12573 return error_mark_node;
12574 }
12575 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12576 {
12577 error_at (OMP_CLAUSE_LOCATION (c),
12578 "length %qE of array section does not have integral type",
12579 length);
12580 return error_mark_node;
12581 }
12582 if (low_bound
12583 && TREE_CODE (low_bound) == INTEGER_CST
12584 && TYPE_PRECISION (TREE_TYPE (low_bound))
12585 > TYPE_PRECISION (sizetype))
12586 low_bound = fold_convert (sizetype, low_bound);
12587 if (length
12588 && TREE_CODE (length) == INTEGER_CST
12589 && TYPE_PRECISION (TREE_TYPE (length))
12590 > TYPE_PRECISION (sizetype))
12591 length = fold_convert (sizetype, length);
12592 if (low_bound == NULL_TREE)
12593 low_bound = integer_zero_node;
12594
12595 if (length != NULL_TREE)
12596 {
12597 if (!integer_nonzerop (length))
12598 {
12599 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12600 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12601 {
12602 if (integer_zerop (length))
12603 {
12604 error_at (OMP_CLAUSE_LOCATION (c),
12605 "zero length array section in %qs clause",
12606 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12607 return error_mark_node;
12608 }
12609 }
12610 else
12611 maybe_zero_len = true;
12612 }
12613 if (first_non_one == types.length ()
12614 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12615 first_non_one++;
12616 }
12617 if (TREE_CODE (type) == ARRAY_TYPE)
12618 {
12619 if (length == NULL_TREE
12620 && (TYPE_DOMAIN (type) == NULL_TREE
12621 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12622 {
12623 error_at (OMP_CLAUSE_LOCATION (c),
12624 "for unknown bound array type length expression must "
12625 "be specified");
12626 return error_mark_node;
12627 }
12628 if (TREE_CODE (low_bound) == INTEGER_CST
12629 && tree_int_cst_sgn (low_bound) == -1)
12630 {
12631 error_at (OMP_CLAUSE_LOCATION (c),
12632 "negative low bound in array section in %qs clause",
12633 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12634 return error_mark_node;
12635 }
12636 if (length != NULL_TREE
12637 && TREE_CODE (length) == INTEGER_CST
12638 && tree_int_cst_sgn (length) == -1)
12639 {
12640 error_at (OMP_CLAUSE_LOCATION (c),
12641 "negative length in array section in %qs clause",
12642 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12643 return error_mark_node;
12644 }
12645 if (TYPE_DOMAIN (type)
12646 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12647 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12648 == INTEGER_CST)
12649 {
12650 tree size
12651 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
12652 size = size_binop (PLUS_EXPR, size, size_one_node);
12653 if (TREE_CODE (low_bound) == INTEGER_CST)
12654 {
12655 if (tree_int_cst_lt (size, low_bound))
12656 {
12657 error_at (OMP_CLAUSE_LOCATION (c),
12658 "low bound %qE above array section size "
12659 "in %qs clause", low_bound,
12660 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12661 return error_mark_node;
12662 }
12663 if (tree_int_cst_equal (size, low_bound))
12664 {
12665 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12666 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12667 {
12668 error_at (OMP_CLAUSE_LOCATION (c),
12669 "zero length array section in %qs clause",
12670 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12671 return error_mark_node;
12672 }
12673 maybe_zero_len = true;
12674 }
12675 else if (length == NULL_TREE
12676 && first_non_one == types.length ()
12677 && tree_int_cst_equal
12678 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12679 low_bound))
12680 first_non_one++;
12681 }
12682 else if (length == NULL_TREE)
12683 {
12684 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12685 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12686 maybe_zero_len = true;
12687 if (first_non_one == types.length ())
12688 first_non_one++;
12689 }
12690 if (length && TREE_CODE (length) == INTEGER_CST)
12691 {
12692 if (tree_int_cst_lt (size, length))
12693 {
12694 error_at (OMP_CLAUSE_LOCATION (c),
12695 "length %qE above array section size "
12696 "in %qs clause", length,
12697 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12698 return error_mark_node;
12699 }
12700 if (TREE_CODE (low_bound) == INTEGER_CST)
12701 {
12702 tree lbpluslen
12703 = size_binop (PLUS_EXPR,
12704 fold_convert (sizetype, low_bound),
12705 fold_convert (sizetype, length));
12706 if (TREE_CODE (lbpluslen) == INTEGER_CST
12707 && tree_int_cst_lt (size, lbpluslen))
12708 {
12709 error_at (OMP_CLAUSE_LOCATION (c),
12710 "high bound %qE above array section size "
12711 "in %qs clause", lbpluslen,
12712 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12713 return error_mark_node;
12714 }
12715 }
12716 }
12717 }
12718 else if (length == NULL_TREE)
12719 {
12720 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12721 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12722 maybe_zero_len = true;
12723 if (first_non_one == types.length ())
12724 first_non_one++;
12725 }
12726
12727 /* For [lb:] we will need to evaluate lb more than once. */
12728 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12729 {
12730 tree lb = save_expr (low_bound);
12731 if (lb != low_bound)
12732 {
12733 TREE_PURPOSE (t) = lb;
12734 low_bound = lb;
12735 }
12736 }
12737 }
12738 else if (TREE_CODE (type) == POINTER_TYPE)
12739 {
12740 if (length == NULL_TREE)
12741 {
12742 error_at (OMP_CLAUSE_LOCATION (c),
12743 "for pointer type length expression must be specified");
12744 return error_mark_node;
12745 }
12746 if (length != NULL_TREE
12747 && TREE_CODE (length) == INTEGER_CST
12748 && tree_int_cst_sgn (length) == -1)
12749 {
12750 error_at (OMP_CLAUSE_LOCATION (c),
12751 "negative length in array section in %qs clause",
12752 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12753 return error_mark_node;
12754 }
12755 /* If there is a pointer type anywhere but in the very first
12756 array-section-subscript, the array section can't be contiguous. */
12757 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12758 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12759 {
12760 error_at (OMP_CLAUSE_LOCATION (c),
12761 "array section is not contiguous in %qs clause",
12762 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12763 return error_mark_node;
12764 }
12765 }
12766 else
12767 {
12768 error_at (OMP_CLAUSE_LOCATION (c),
12769 "%qE does not have pointer or array type", ret);
12770 return error_mark_node;
12771 }
12772 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12773 types.safe_push (TREE_TYPE (ret));
12774 /* We will need to evaluate lb more than once. */
12775 tree lb = save_expr (low_bound);
12776 if (lb != low_bound)
12777 {
12778 TREE_PURPOSE (t) = lb;
12779 low_bound = lb;
12780 }
12781 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12782 return ret;
12783 }
12784
12785 /* Handle array sections for clause C. */
12786
12787 static bool
handle_omp_array_sections(tree c,enum c_omp_region_type ort)12788 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12789 {
12790 bool maybe_zero_len = false;
12791 unsigned int first_non_one = 0;
12792 auto_vec<tree, 10> types;
12793 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12794 maybe_zero_len, first_non_one,
12795 ort);
12796 if (first == error_mark_node)
12797 return true;
12798 if (first == NULL_TREE)
12799 return false;
12800 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12801 {
12802 tree t = OMP_CLAUSE_DECL (c);
12803 tree tem = NULL_TREE;
12804 /* Need to evaluate side effects in the length expressions
12805 if any. */
12806 while (TREE_CODE (t) == TREE_LIST)
12807 {
12808 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12809 {
12810 if (tem == NULL_TREE)
12811 tem = TREE_VALUE (t);
12812 else
12813 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12814 TREE_VALUE (t), tem);
12815 }
12816 t = TREE_CHAIN (t);
12817 }
12818 if (tem)
12819 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12820 first = c_fully_fold (first, false, NULL, true);
12821 OMP_CLAUSE_DECL (c) = first;
12822 }
12823 else
12824 {
12825 unsigned int num = types.length (), i;
12826 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12827 tree condition = NULL_TREE;
12828
12829 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12830 maybe_zero_len = true;
12831
12832 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12833 t = TREE_CHAIN (t))
12834 {
12835 tree low_bound = TREE_PURPOSE (t);
12836 tree length = TREE_VALUE (t);
12837
12838 i--;
12839 if (low_bound
12840 && TREE_CODE (low_bound) == INTEGER_CST
12841 && TYPE_PRECISION (TREE_TYPE (low_bound))
12842 > TYPE_PRECISION (sizetype))
12843 low_bound = fold_convert (sizetype, low_bound);
12844 if (length
12845 && TREE_CODE (length) == INTEGER_CST
12846 && TYPE_PRECISION (TREE_TYPE (length))
12847 > TYPE_PRECISION (sizetype))
12848 length = fold_convert (sizetype, length);
12849 if (low_bound == NULL_TREE)
12850 low_bound = integer_zero_node;
12851 if (!maybe_zero_len && i > first_non_one)
12852 {
12853 if (integer_nonzerop (low_bound))
12854 goto do_warn_noncontiguous;
12855 if (length != NULL_TREE
12856 && TREE_CODE (length) == INTEGER_CST
12857 && TYPE_DOMAIN (types[i])
12858 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12859 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12860 == INTEGER_CST)
12861 {
12862 tree size;
12863 size = size_binop (PLUS_EXPR,
12864 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12865 size_one_node);
12866 if (!tree_int_cst_equal (length, size))
12867 {
12868 do_warn_noncontiguous:
12869 error_at (OMP_CLAUSE_LOCATION (c),
12870 "array section is not contiguous in %qs "
12871 "clause",
12872 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12873 return true;
12874 }
12875 }
12876 if (length != NULL_TREE
12877 && TREE_SIDE_EFFECTS (length))
12878 {
12879 if (side_effects == NULL_TREE)
12880 side_effects = length;
12881 else
12882 side_effects = build2 (COMPOUND_EXPR,
12883 TREE_TYPE (side_effects),
12884 length, side_effects);
12885 }
12886 }
12887 else
12888 {
12889 tree l;
12890
12891 if (i > first_non_one
12892 && ((length && integer_nonzerop (length))
12893 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12894 continue;
12895 if (length)
12896 l = fold_convert (sizetype, length);
12897 else
12898 {
12899 l = size_binop (PLUS_EXPR,
12900 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12901 size_one_node);
12902 l = size_binop (MINUS_EXPR, l,
12903 fold_convert (sizetype, low_bound));
12904 }
12905 if (i > first_non_one)
12906 {
12907 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12908 size_zero_node);
12909 if (condition == NULL_TREE)
12910 condition = l;
12911 else
12912 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12913 l, condition);
12914 }
12915 else if (size == NULL_TREE)
12916 {
12917 size = size_in_bytes (TREE_TYPE (types[i]));
12918 tree eltype = TREE_TYPE (types[num - 1]);
12919 while (TREE_CODE (eltype) == ARRAY_TYPE)
12920 eltype = TREE_TYPE (eltype);
12921 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12922 {
12923 if (integer_zerop (size)
12924 || integer_zerop (size_in_bytes (eltype)))
12925 {
12926 error_at (OMP_CLAUSE_LOCATION (c),
12927 "zero length array section in %qs clause",
12928 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12929 return error_mark_node;
12930 }
12931 size = size_binop (EXACT_DIV_EXPR, size,
12932 size_in_bytes (eltype));
12933 }
12934 size = size_binop (MULT_EXPR, size, l);
12935 if (condition)
12936 size = fold_build3 (COND_EXPR, sizetype, condition,
12937 size, size_zero_node);
12938 }
12939 else
12940 size = size_binop (MULT_EXPR, size, l);
12941 }
12942 }
12943 if (side_effects)
12944 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12945 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12946 {
12947 size = size_binop (MINUS_EXPR, size, size_one_node);
12948 size = c_fully_fold (size, false, NULL);
12949 tree index_type = build_index_type (size);
12950 tree eltype = TREE_TYPE (first);
12951 while (TREE_CODE (eltype) == ARRAY_TYPE)
12952 eltype = TREE_TYPE (eltype);
12953 tree type = build_array_type (eltype, index_type);
12954 tree ptype = build_pointer_type (eltype);
12955 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12956 t = build_fold_addr_expr (t);
12957 tree t2 = build_fold_addr_expr (first);
12958 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12959 ptrdiff_type_node, t2);
12960 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12961 ptrdiff_type_node, t2,
12962 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12963 ptrdiff_type_node, t));
12964 t2 = c_fully_fold (t2, false, NULL);
12965 if (tree_fits_shwi_p (t2))
12966 t = build2 (MEM_REF, type, t,
12967 build_int_cst (ptype, tree_to_shwi (t2)));
12968 else
12969 {
12970 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12971 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12972 TREE_TYPE (t), t, t2);
12973 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12974 }
12975 OMP_CLAUSE_DECL (c) = t;
12976 return false;
12977 }
12978 first = c_fully_fold (first, false, NULL);
12979 OMP_CLAUSE_DECL (c) = first;
12980 if (size)
12981 size = c_fully_fold (size, false, NULL);
12982 OMP_CLAUSE_SIZE (c) = size;
12983 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12984 || (TREE_CODE (t) == COMPONENT_REF
12985 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12986 return false;
12987 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12988 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
12989 switch (OMP_CLAUSE_MAP_KIND (c))
12990 {
12991 case GOMP_MAP_ALLOC:
12992 case GOMP_MAP_TO:
12993 case GOMP_MAP_FROM:
12994 case GOMP_MAP_TOFROM:
12995 case GOMP_MAP_ALWAYS_TO:
12996 case GOMP_MAP_ALWAYS_FROM:
12997 case GOMP_MAP_ALWAYS_TOFROM:
12998 case GOMP_MAP_RELEASE:
12999 case GOMP_MAP_DELETE:
13000 case GOMP_MAP_FORCE_TO:
13001 case GOMP_MAP_FORCE_FROM:
13002 case GOMP_MAP_FORCE_TOFROM:
13003 case GOMP_MAP_FORCE_PRESENT:
13004 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13005 break;
13006 default:
13007 break;
13008 }
13009 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13010 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
13011 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
13012 else if (TREE_CODE (t) == COMPONENT_REF)
13013 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
13014 else
13015 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13016 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13017 && !c_mark_addressable (t))
13018 return false;
13019 OMP_CLAUSE_DECL (c2) = t;
13020 t = build_fold_addr_expr (first);
13021 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13022 tree ptr = OMP_CLAUSE_DECL (c2);
13023 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13024 ptr = build_fold_addr_expr (ptr);
13025 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13026 ptrdiff_type_node, t,
13027 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13028 ptrdiff_type_node, ptr));
13029 t = c_fully_fold (t, false, NULL);
13030 OMP_CLAUSE_SIZE (c2) = t;
13031 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13032 OMP_CLAUSE_CHAIN (c) = c2;
13033 }
13034 return false;
13035 }
13036
13037 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13038 an inline call. But, remap
13039 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13040 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13041
13042 static tree
c_clone_omp_udr(tree stmt,tree omp_decl1,tree omp_decl2,tree decl,tree placeholder)13043 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13044 tree decl, tree placeholder)
13045 {
13046 copy_body_data id;
13047 hash_map<tree, tree> decl_map;
13048
13049 decl_map.put (omp_decl1, placeholder);
13050 decl_map.put (omp_decl2, decl);
13051 memset (&id, 0, sizeof (id));
13052 id.src_fn = DECL_CONTEXT (omp_decl1);
13053 id.dst_fn = current_function_decl;
13054 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13055 id.decl_map = &decl_map;
13056
13057 id.copy_decl = copy_decl_no_change;
13058 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13059 id.transform_new_cfg = true;
13060 id.transform_return_to_modify = false;
13061 id.transform_lang_insert_block = NULL;
13062 id.eh_lp_nr = 0;
13063 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13064 return stmt;
13065 }
13066
13067 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13068 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13069
13070 static tree
c_find_omp_placeholder_r(tree * tp,int *,void * data)13071 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13072 {
13073 if (*tp == (tree) data)
13074 return *tp;
13075 return NULL_TREE;
13076 }
13077
13078 /* For all elements of CLAUSES, validate them against their constraints.
13079 Remove any elements from the list that are invalid. */
13080
13081 tree
c_finish_omp_clauses(tree clauses,enum c_omp_region_type ort)13082 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
13083 {
13084 bitmap_head generic_head, firstprivate_head, lastprivate_head;
13085 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
13086 tree c, t, type, *pc;
13087 tree simdlen = NULL_TREE, safelen = NULL_TREE;
13088 bool branch_seen = false;
13089 bool copyprivate_seen = false;
13090 bool linear_variable_step_check = false;
13091 tree *nowait_clause = NULL;
13092 bool ordered_seen = false;
13093 tree schedule_clause = NULL_TREE;
13094 bool oacc_async = false;
13095
13096 bitmap_obstack_initialize (NULL);
13097 bitmap_initialize (&generic_head, &bitmap_default_obstack);
13098 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
13099 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
13100 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
13101 bitmap_initialize (&map_head, &bitmap_default_obstack);
13102 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
13103 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
13104
13105 if (ort & C_ORT_ACC)
13106 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
13107 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
13108 {
13109 oacc_async = true;
13110 break;
13111 }
13112
13113 for (pc = &clauses, c = clauses; c ; c = *pc)
13114 {
13115 bool remove = false;
13116 bool need_complete = false;
13117 bool need_implicitly_determined = false;
13118
13119 switch (OMP_CLAUSE_CODE (c))
13120 {
13121 case OMP_CLAUSE_SHARED:
13122 need_implicitly_determined = true;
13123 goto check_dup_generic;
13124
13125 case OMP_CLAUSE_PRIVATE:
13126 need_complete = true;
13127 need_implicitly_determined = true;
13128 goto check_dup_generic;
13129
13130 case OMP_CLAUSE_REDUCTION:
13131 need_implicitly_determined = true;
13132 t = OMP_CLAUSE_DECL (c);
13133 if (TREE_CODE (t) == TREE_LIST)
13134 {
13135 if (handle_omp_array_sections (c, ort))
13136 {
13137 remove = true;
13138 break;
13139 }
13140
13141 t = OMP_CLAUSE_DECL (c);
13142 }
13143 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13144 if (t == error_mark_node)
13145 {
13146 remove = true;
13147 break;
13148 }
13149 if (oacc_async)
13150 c_mark_addressable (t);
13151 type = TREE_TYPE (t);
13152 if (TREE_CODE (t) == MEM_REF)
13153 type = TREE_TYPE (type);
13154 if (TREE_CODE (type) == ARRAY_TYPE)
13155 {
13156 tree oatype = type;
13157 gcc_assert (TREE_CODE (t) != MEM_REF);
13158 while (TREE_CODE (type) == ARRAY_TYPE)
13159 type = TREE_TYPE (type);
13160 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13161 {
13162 error_at (OMP_CLAUSE_LOCATION (c),
13163 "%qD in %<reduction%> clause is a zero size array",
13164 t);
13165 remove = true;
13166 break;
13167 }
13168 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
13169 TYPE_SIZE_UNIT (type));
13170 if (integer_zerop (size))
13171 {
13172 error_at (OMP_CLAUSE_LOCATION (c),
13173 "%qD in %<reduction%> clause is a zero size array",
13174 t);
13175 remove = true;
13176 break;
13177 }
13178 size = size_binop (MINUS_EXPR, size, size_one_node);
13179 tree index_type = build_index_type (size);
13180 tree atype = build_array_type (type, index_type);
13181 tree ptype = build_pointer_type (type);
13182 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13183 t = build_fold_addr_expr (t);
13184 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
13185 OMP_CLAUSE_DECL (c) = t;
13186 }
13187 if (TYPE_ATOMIC (type))
13188 {
13189 error_at (OMP_CLAUSE_LOCATION (c),
13190 "%<_Atomic%> %qE in %<reduction%> clause", t);
13191 remove = true;
13192 break;
13193 }
13194 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
13195 && (FLOAT_TYPE_P (type)
13196 || TREE_CODE (type) == COMPLEX_TYPE))
13197 {
13198 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
13199 const char *r_name = NULL;
13200
13201 switch (r_code)
13202 {
13203 case PLUS_EXPR:
13204 case MULT_EXPR:
13205 case MINUS_EXPR:
13206 break;
13207 case MIN_EXPR:
13208 if (TREE_CODE (type) == COMPLEX_TYPE)
13209 r_name = "min";
13210 break;
13211 case MAX_EXPR:
13212 if (TREE_CODE (type) == COMPLEX_TYPE)
13213 r_name = "max";
13214 break;
13215 case BIT_AND_EXPR:
13216 r_name = "&";
13217 break;
13218 case BIT_XOR_EXPR:
13219 r_name = "^";
13220 break;
13221 case BIT_IOR_EXPR:
13222 r_name = "|";
13223 break;
13224 case TRUTH_ANDIF_EXPR:
13225 if (FLOAT_TYPE_P (type))
13226 r_name = "&&";
13227 break;
13228 case TRUTH_ORIF_EXPR:
13229 if (FLOAT_TYPE_P (type))
13230 r_name = "||";
13231 break;
13232 default:
13233 gcc_unreachable ();
13234 }
13235 if (r_name)
13236 {
13237 error_at (OMP_CLAUSE_LOCATION (c),
13238 "%qE has invalid type for %<reduction(%s)%>",
13239 t, r_name);
13240 remove = true;
13241 break;
13242 }
13243 }
13244 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
13245 {
13246 error_at (OMP_CLAUSE_LOCATION (c),
13247 "user defined reduction not found for %qE", t);
13248 remove = true;
13249 break;
13250 }
13251 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13252 {
13253 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
13254 type = TYPE_MAIN_VARIANT (type);
13255 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13256 VAR_DECL, NULL_TREE, type);
13257 tree decl_placeholder = NULL_TREE;
13258 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
13259 DECL_ARTIFICIAL (placeholder) = 1;
13260 DECL_IGNORED_P (placeholder) = 1;
13261 if (TREE_CODE (t) == MEM_REF)
13262 {
13263 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13264 VAR_DECL, NULL_TREE, type);
13265 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
13266 DECL_ARTIFICIAL (decl_placeholder) = 1;
13267 DECL_IGNORED_P (decl_placeholder) = 1;
13268 }
13269 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
13270 c_mark_addressable (placeholder);
13271 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
13272 c_mark_addressable (decl_placeholder ? decl_placeholder
13273 : OMP_CLAUSE_DECL (c));
13274 OMP_CLAUSE_REDUCTION_MERGE (c)
13275 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
13276 TREE_VEC_ELT (list, 0),
13277 TREE_VEC_ELT (list, 1),
13278 decl_placeholder ? decl_placeholder
13279 : OMP_CLAUSE_DECL (c), placeholder);
13280 OMP_CLAUSE_REDUCTION_MERGE (c)
13281 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13282 void_type_node, NULL_TREE,
13283 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
13284 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13285 if (TREE_VEC_LENGTH (list) == 6)
13286 {
13287 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
13288 c_mark_addressable (decl_placeholder ? decl_placeholder
13289 : OMP_CLAUSE_DECL (c));
13290 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13291 c_mark_addressable (placeholder);
13292 tree init = TREE_VEC_ELT (list, 5);
13293 if (init == error_mark_node)
13294 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13295 OMP_CLAUSE_REDUCTION_INIT (c)
13296 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13297 TREE_VEC_ELT (list, 3),
13298 decl_placeholder ? decl_placeholder
13299 : OMP_CLAUSE_DECL (c), placeholder);
13300 if (TREE_VEC_ELT (list, 5) == error_mark_node)
13301 {
13302 tree v = decl_placeholder ? decl_placeholder : t;
13303 OMP_CLAUSE_REDUCTION_INIT (c)
13304 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13305 OMP_CLAUSE_REDUCTION_INIT (c));
13306 }
13307 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13308 c_find_omp_placeholder_r,
13309 placeholder, NULL))
13310 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13311 }
13312 else
13313 {
13314 tree init;
13315 tree v = decl_placeholder ? decl_placeholder : t;
13316 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13317 init = build_constructor (TREE_TYPE (v), NULL);
13318 else
13319 init = fold_convert (TREE_TYPE (v), integer_zero_node);
13320 OMP_CLAUSE_REDUCTION_INIT (c)
13321 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
13322 }
13323 OMP_CLAUSE_REDUCTION_INIT (c)
13324 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13325 void_type_node, NULL_TREE,
13326 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13327 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13328 }
13329 if (TREE_CODE (t) == MEM_REF)
13330 {
13331 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13332 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13333 != INTEGER_CST)
13334 {
13335 sorry ("variable length element type in array "
13336 "%<reduction%> clause");
13337 remove = true;
13338 break;
13339 }
13340 t = TREE_OPERAND (t, 0);
13341 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13342 t = TREE_OPERAND (t, 0);
13343 if (TREE_CODE (t) == ADDR_EXPR)
13344 t = TREE_OPERAND (t, 0);
13345 }
13346 goto check_dup_generic_t;
13347
13348 case OMP_CLAUSE_COPYPRIVATE:
13349 copyprivate_seen = true;
13350 if (nowait_clause)
13351 {
13352 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13353 "%<nowait%> clause must not be used together "
13354 "with %<copyprivate%>");
13355 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13356 nowait_clause = NULL;
13357 }
13358 goto check_dup_generic;
13359
13360 case OMP_CLAUSE_COPYIN:
13361 t = OMP_CLAUSE_DECL (c);
13362 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
13363 {
13364 error_at (OMP_CLAUSE_LOCATION (c),
13365 "%qE must be %<threadprivate%> for %<copyin%>", t);
13366 remove = true;
13367 break;
13368 }
13369 goto check_dup_generic;
13370
13371 case OMP_CLAUSE_LINEAR:
13372 if (ort != C_ORT_OMP_DECLARE_SIMD)
13373 need_implicitly_determined = true;
13374 t = OMP_CLAUSE_DECL (c);
13375 if (ort != C_ORT_OMP_DECLARE_SIMD
13376 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13377 {
13378 error_at (OMP_CLAUSE_LOCATION (c),
13379 "modifier should not be specified in %<linear%> "
13380 "clause on %<simd%> or %<for%> constructs");
13381 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13382 }
13383 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13384 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13385 {
13386 error_at (OMP_CLAUSE_LOCATION (c),
13387 "linear clause applied to non-integral non-pointer "
13388 "variable with type %qT", TREE_TYPE (t));
13389 remove = true;
13390 break;
13391 }
13392 if (TYPE_ATOMIC (TREE_TYPE (t)))
13393 {
13394 error_at (OMP_CLAUSE_LOCATION (c),
13395 "%<_Atomic%> %qD in %<linear%> clause", t);
13396 remove = true;
13397 break;
13398 }
13399 if (ort == C_ORT_OMP_DECLARE_SIMD)
13400 {
13401 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13402 if (TREE_CODE (s) == PARM_DECL)
13403 {
13404 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13405 /* map_head bitmap is used as uniform_head if
13406 declare_simd. */
13407 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13408 linear_variable_step_check = true;
13409 goto check_dup_generic;
13410 }
13411 if (TREE_CODE (s) != INTEGER_CST)
13412 {
13413 error_at (OMP_CLAUSE_LOCATION (c),
13414 "%<linear%> clause step %qE is neither constant "
13415 "nor a parameter", s);
13416 remove = true;
13417 break;
13418 }
13419 }
13420 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13421 {
13422 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13423 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13424 OMP_CLAUSE_DECL (c), s);
13425 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13426 sizetype, fold_convert (sizetype, s),
13427 fold_convert
13428 (sizetype, OMP_CLAUSE_DECL (c)));
13429 if (s == error_mark_node)
13430 s = size_one_node;
13431 OMP_CLAUSE_LINEAR_STEP (c) = s;
13432 }
13433 else
13434 OMP_CLAUSE_LINEAR_STEP (c)
13435 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
13436 goto check_dup_generic;
13437
13438 check_dup_generic:
13439 t = OMP_CLAUSE_DECL (c);
13440 check_dup_generic_t:
13441 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13442 {
13443 error_at (OMP_CLAUSE_LOCATION (c),
13444 "%qE is not a variable in clause %qs", t,
13445 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13446 remove = true;
13447 }
13448 else if (ort == C_ORT_ACC
13449 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13450 {
13451 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13452 {
13453 error ("%qD appears more than once in reduction clauses", t);
13454 remove = true;
13455 }
13456 else
13457 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13458 }
13459 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13460 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13461 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13462 {
13463 error_at (OMP_CLAUSE_LOCATION (c),
13464 "%qE appears more than once in data clauses", t);
13465 remove = true;
13466 }
13467 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13468 && bitmap_bit_p (&map_head, DECL_UID (t)))
13469 {
13470 if (ort == C_ORT_ACC)
13471 error ("%qD appears more than once in data clauses", t);
13472 else
13473 error ("%qD appears both in data and map clauses", t);
13474 remove = true;
13475 }
13476 else
13477 bitmap_set_bit (&generic_head, DECL_UID (t));
13478 break;
13479
13480 case OMP_CLAUSE_FIRSTPRIVATE:
13481 t = OMP_CLAUSE_DECL (c);
13482 need_complete = true;
13483 need_implicitly_determined = true;
13484 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13485 {
13486 error_at (OMP_CLAUSE_LOCATION (c),
13487 "%qE is not a variable in clause %<firstprivate%>", t);
13488 remove = true;
13489 }
13490 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13491 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13492 {
13493 error_at (OMP_CLAUSE_LOCATION (c),
13494 "%qE appears more than once in data clauses", t);
13495 remove = true;
13496 }
13497 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13498 {
13499 if (ort == C_ORT_ACC)
13500 error ("%qD appears more than once in data clauses", t);
13501 else
13502 error ("%qD appears both in data and map clauses", t);
13503 remove = true;
13504 }
13505 else
13506 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13507 break;
13508
13509 case OMP_CLAUSE_LASTPRIVATE:
13510 t = OMP_CLAUSE_DECL (c);
13511 need_complete = true;
13512 need_implicitly_determined = true;
13513 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13514 {
13515 error_at (OMP_CLAUSE_LOCATION (c),
13516 "%qE is not a variable in clause %<lastprivate%>", t);
13517 remove = true;
13518 }
13519 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13520 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13521 {
13522 error_at (OMP_CLAUSE_LOCATION (c),
13523 "%qE appears more than once in data clauses", t);
13524 remove = true;
13525 }
13526 else
13527 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13528 break;
13529
13530 case OMP_CLAUSE_ALIGNED:
13531 t = OMP_CLAUSE_DECL (c);
13532 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13533 {
13534 error_at (OMP_CLAUSE_LOCATION (c),
13535 "%qE is not a variable in %<aligned%> clause", t);
13536 remove = true;
13537 }
13538 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13539 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13540 {
13541 error_at (OMP_CLAUSE_LOCATION (c),
13542 "%qE in %<aligned%> clause is neither a pointer nor "
13543 "an array", t);
13544 remove = true;
13545 }
13546 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13547 {
13548 error_at (OMP_CLAUSE_LOCATION (c),
13549 "%<_Atomic%> %qD in %<aligned%> clause", t);
13550 remove = true;
13551 break;
13552 }
13553 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13554 {
13555 error_at (OMP_CLAUSE_LOCATION (c),
13556 "%qE appears more than once in %<aligned%> clauses",
13557 t);
13558 remove = true;
13559 }
13560 else
13561 bitmap_set_bit (&aligned_head, DECL_UID (t));
13562 break;
13563
13564 case OMP_CLAUSE_DEPEND:
13565 t = OMP_CLAUSE_DECL (c);
13566 if (t == NULL_TREE)
13567 {
13568 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13569 == OMP_CLAUSE_DEPEND_SOURCE);
13570 break;
13571 }
13572 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13573 {
13574 gcc_assert (TREE_CODE (t) == TREE_LIST);
13575 for (; t; t = TREE_CHAIN (t))
13576 {
13577 tree decl = TREE_VALUE (t);
13578 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13579 {
13580 tree offset = TREE_PURPOSE (t);
13581 bool neg = wi::neg_p (wi::to_wide (offset));
13582 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13583 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13584 neg ? MINUS_EXPR : PLUS_EXPR,
13585 decl, offset);
13586 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13587 sizetype,
13588 fold_convert (sizetype, t2),
13589 fold_convert (sizetype, decl));
13590 if (t2 == error_mark_node)
13591 {
13592 remove = true;
13593 break;
13594 }
13595 TREE_PURPOSE (t) = t2;
13596 }
13597 }
13598 break;
13599 }
13600 if (TREE_CODE (t) == TREE_LIST)
13601 {
13602 if (handle_omp_array_sections (c, ort))
13603 remove = true;
13604 break;
13605 }
13606 if (t == error_mark_node)
13607 remove = true;
13608 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13609 {
13610 error_at (OMP_CLAUSE_LOCATION (c),
13611 "%qE is not a variable in %<depend%> clause", t);
13612 remove = true;
13613 }
13614 else if (!c_mark_addressable (t))
13615 remove = true;
13616 break;
13617
13618 case OMP_CLAUSE_MAP:
13619 case OMP_CLAUSE_TO:
13620 case OMP_CLAUSE_FROM:
13621 case OMP_CLAUSE__CACHE_:
13622 t = OMP_CLAUSE_DECL (c);
13623 if (TREE_CODE (t) == TREE_LIST)
13624 {
13625 if (handle_omp_array_sections (c, ort))
13626 remove = true;
13627 else
13628 {
13629 t = OMP_CLAUSE_DECL (c);
13630 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13631 {
13632 error_at (OMP_CLAUSE_LOCATION (c),
13633 "array section does not have mappable type "
13634 "in %qs clause",
13635 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13636 remove = true;
13637 }
13638 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13639 {
13640 error_at (OMP_CLAUSE_LOCATION (c),
13641 "%<_Atomic%> %qE in %qs clause", t,
13642 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13643 remove = true;
13644 }
13645 while (TREE_CODE (t) == ARRAY_REF)
13646 t = TREE_OPERAND (t, 0);
13647 if (TREE_CODE (t) == COMPONENT_REF
13648 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13649 {
13650 while (TREE_CODE (t) == COMPONENT_REF)
13651 t = TREE_OPERAND (t, 0);
13652 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13653 break;
13654 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13655 {
13656 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13657 error ("%qD appears more than once in motion"
13658 " clauses", t);
13659 else if (ort == C_ORT_ACC)
13660 error ("%qD appears more than once in data"
13661 " clauses", t);
13662 else
13663 error ("%qD appears more than once in map"
13664 " clauses", t);
13665 remove = true;
13666 }
13667 else
13668 {
13669 bitmap_set_bit (&map_head, DECL_UID (t));
13670 bitmap_set_bit (&map_field_head, DECL_UID (t));
13671 }
13672 }
13673 }
13674 break;
13675 }
13676 if (t == error_mark_node)
13677 {
13678 remove = true;
13679 break;
13680 }
13681 if (TREE_CODE (t) == COMPONENT_REF
13682 && (ort & C_ORT_OMP)
13683 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13684 {
13685 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13686 {
13687 error_at (OMP_CLAUSE_LOCATION (c),
13688 "bit-field %qE in %qs clause",
13689 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13690 remove = true;
13691 }
13692 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13693 {
13694 error_at (OMP_CLAUSE_LOCATION (c),
13695 "%qE does not have a mappable type in %qs clause",
13696 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13697 remove = true;
13698 }
13699 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13700 {
13701 error_at (OMP_CLAUSE_LOCATION (c),
13702 "%<_Atomic%> %qE in %qs clause", t,
13703 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13704 remove = true;
13705 }
13706 while (TREE_CODE (t) == COMPONENT_REF)
13707 {
13708 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13709 == UNION_TYPE)
13710 {
13711 error_at (OMP_CLAUSE_LOCATION (c),
13712 "%qE is a member of a union", t);
13713 remove = true;
13714 break;
13715 }
13716 t = TREE_OPERAND (t, 0);
13717 }
13718 if (remove)
13719 break;
13720 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13721 {
13722 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13723 break;
13724 }
13725 }
13726 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13727 {
13728 error_at (OMP_CLAUSE_LOCATION (c),
13729 "%qE is not a variable in %qs clause", t,
13730 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13731 remove = true;
13732 }
13733 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13734 {
13735 error_at (OMP_CLAUSE_LOCATION (c),
13736 "%qD is threadprivate variable in %qs clause", t,
13737 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13738 remove = true;
13739 }
13740 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13741 || (OMP_CLAUSE_MAP_KIND (c)
13742 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13743 && !c_mark_addressable (t))
13744 remove = true;
13745 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13746 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13747 || (OMP_CLAUSE_MAP_KIND (c)
13748 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13749 || (OMP_CLAUSE_MAP_KIND (c)
13750 == GOMP_MAP_FORCE_DEVICEPTR)))
13751 && t == OMP_CLAUSE_DECL (c)
13752 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13753 {
13754 error_at (OMP_CLAUSE_LOCATION (c),
13755 "%qD does not have a mappable type in %qs clause", t,
13756 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13757 remove = true;
13758 }
13759 else if (TREE_TYPE (t) == error_mark_node)
13760 remove = true;
13761 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13762 {
13763 error_at (OMP_CLAUSE_LOCATION (c),
13764 "%<_Atomic%> %qE in %qs clause", t,
13765 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13766 remove = true;
13767 }
13768 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13769 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13770 {
13771 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13772 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13773 {
13774 error ("%qD appears more than once in data clauses", t);
13775 remove = true;
13776 }
13777 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13778 {
13779 if (ort == C_ORT_ACC)
13780 error ("%qD appears more than once in data clauses", t);
13781 else
13782 error ("%qD appears both in data and map clauses", t);
13783 remove = true;
13784 }
13785 else
13786 bitmap_set_bit (&generic_head, DECL_UID (t));
13787 }
13788 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13789 {
13790 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13791 error ("%qD appears more than once in motion clauses", t);
13792 else if (ort == C_ORT_ACC)
13793 error ("%qD appears more than once in data clauses", t);
13794 else
13795 error ("%qD appears more than once in map clauses", t);
13796 remove = true;
13797 }
13798 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13799 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13800 {
13801 if (ort == C_ORT_ACC)
13802 error ("%qD appears more than once in data clauses", t);
13803 else
13804 error ("%qD appears both in data and map clauses", t);
13805 remove = true;
13806 }
13807 else
13808 {
13809 bitmap_set_bit (&map_head, DECL_UID (t));
13810 if (t != OMP_CLAUSE_DECL (c)
13811 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13812 bitmap_set_bit (&map_field_head, DECL_UID (t));
13813 }
13814 break;
13815
13816 case OMP_CLAUSE_TO_DECLARE:
13817 case OMP_CLAUSE_LINK:
13818 t = OMP_CLAUSE_DECL (c);
13819 if (TREE_CODE (t) == FUNCTION_DECL
13820 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13821 ;
13822 else if (!VAR_P (t))
13823 {
13824 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13825 error_at (OMP_CLAUSE_LOCATION (c),
13826 "%qE is neither a variable nor a function name in "
13827 "clause %qs", t,
13828 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13829 else
13830 error_at (OMP_CLAUSE_LOCATION (c),
13831 "%qE is not a variable in clause %qs", t,
13832 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13833 remove = true;
13834 }
13835 else if (DECL_THREAD_LOCAL_P (t))
13836 {
13837 error_at (OMP_CLAUSE_LOCATION (c),
13838 "%qD is threadprivate variable in %qs clause", t,
13839 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13840 remove = true;
13841 }
13842 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13843 {
13844 error_at (OMP_CLAUSE_LOCATION (c),
13845 "%qD does not have a mappable type in %qs clause", t,
13846 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13847 remove = true;
13848 }
13849 if (remove)
13850 break;
13851 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13852 {
13853 error_at (OMP_CLAUSE_LOCATION (c),
13854 "%qE appears more than once on the same "
13855 "%<declare target%> directive", t);
13856 remove = true;
13857 }
13858 else
13859 bitmap_set_bit (&generic_head, DECL_UID (t));
13860 break;
13861
13862 case OMP_CLAUSE_UNIFORM:
13863 t = OMP_CLAUSE_DECL (c);
13864 if (TREE_CODE (t) != PARM_DECL)
13865 {
13866 if (DECL_P (t))
13867 error_at (OMP_CLAUSE_LOCATION (c),
13868 "%qD is not an argument in %<uniform%> clause", t);
13869 else
13870 error_at (OMP_CLAUSE_LOCATION (c),
13871 "%qE is not an argument in %<uniform%> clause", t);
13872 remove = true;
13873 break;
13874 }
13875 /* map_head bitmap is used as uniform_head if declare_simd. */
13876 bitmap_set_bit (&map_head, DECL_UID (t));
13877 goto check_dup_generic;
13878
13879 case OMP_CLAUSE_IS_DEVICE_PTR:
13880 case OMP_CLAUSE_USE_DEVICE_PTR:
13881 t = OMP_CLAUSE_DECL (c);
13882 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13883 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13884 {
13885 error_at (OMP_CLAUSE_LOCATION (c),
13886 "%qs variable is neither a pointer nor an array",
13887 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13888 remove = true;
13889 }
13890 goto check_dup_generic;
13891
13892 case OMP_CLAUSE_NOWAIT:
13893 if (copyprivate_seen)
13894 {
13895 error_at (OMP_CLAUSE_LOCATION (c),
13896 "%<nowait%> clause must not be used together "
13897 "with %<copyprivate%>");
13898 remove = true;
13899 break;
13900 }
13901 nowait_clause = pc;
13902 pc = &OMP_CLAUSE_CHAIN (c);
13903 continue;
13904
13905 case OMP_CLAUSE_IF:
13906 case OMP_CLAUSE_NUM_THREADS:
13907 case OMP_CLAUSE_NUM_TEAMS:
13908 case OMP_CLAUSE_THREAD_LIMIT:
13909 case OMP_CLAUSE_DEFAULT:
13910 case OMP_CLAUSE_UNTIED:
13911 case OMP_CLAUSE_COLLAPSE:
13912 case OMP_CLAUSE_FINAL:
13913 case OMP_CLAUSE_MERGEABLE:
13914 case OMP_CLAUSE_DEVICE:
13915 case OMP_CLAUSE_DIST_SCHEDULE:
13916 case OMP_CLAUSE_PARALLEL:
13917 case OMP_CLAUSE_FOR:
13918 case OMP_CLAUSE_SECTIONS:
13919 case OMP_CLAUSE_TASKGROUP:
13920 case OMP_CLAUSE_PROC_BIND:
13921 case OMP_CLAUSE_PRIORITY:
13922 case OMP_CLAUSE_GRAINSIZE:
13923 case OMP_CLAUSE_NUM_TASKS:
13924 case OMP_CLAUSE_NOGROUP:
13925 case OMP_CLAUSE_THREADS:
13926 case OMP_CLAUSE_SIMD:
13927 case OMP_CLAUSE_HINT:
13928 case OMP_CLAUSE_DEFAULTMAP:
13929 case OMP_CLAUSE_NUM_GANGS:
13930 case OMP_CLAUSE_NUM_WORKERS:
13931 case OMP_CLAUSE_VECTOR_LENGTH:
13932 case OMP_CLAUSE_ASYNC:
13933 case OMP_CLAUSE_WAIT:
13934 case OMP_CLAUSE_AUTO:
13935 case OMP_CLAUSE_INDEPENDENT:
13936 case OMP_CLAUSE_SEQ:
13937 case OMP_CLAUSE_GANG:
13938 case OMP_CLAUSE_WORKER:
13939 case OMP_CLAUSE_VECTOR:
13940 case OMP_CLAUSE_TILE:
13941 pc = &OMP_CLAUSE_CHAIN (c);
13942 continue;
13943
13944 case OMP_CLAUSE_SCHEDULE:
13945 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13946 {
13947 const char *p = NULL;
13948 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13949 {
13950 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13951 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13952 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13953 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13954 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13955 default: gcc_unreachable ();
13956 }
13957 if (p)
13958 {
13959 error_at (OMP_CLAUSE_LOCATION (c),
13960 "%<nonmonotonic%> modifier specified for %qs "
13961 "schedule kind", p);
13962 OMP_CLAUSE_SCHEDULE_KIND (c)
13963 = (enum omp_clause_schedule_kind)
13964 (OMP_CLAUSE_SCHEDULE_KIND (c)
13965 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13966 }
13967 }
13968 schedule_clause = c;
13969 pc = &OMP_CLAUSE_CHAIN (c);
13970 continue;
13971
13972 case OMP_CLAUSE_ORDERED:
13973 ordered_seen = true;
13974 pc = &OMP_CLAUSE_CHAIN (c);
13975 continue;
13976
13977 case OMP_CLAUSE_SAFELEN:
13978 safelen = c;
13979 pc = &OMP_CLAUSE_CHAIN (c);
13980 continue;
13981 case OMP_CLAUSE_SIMDLEN:
13982 simdlen = c;
13983 pc = &OMP_CLAUSE_CHAIN (c);
13984 continue;
13985
13986 case OMP_CLAUSE_INBRANCH:
13987 case OMP_CLAUSE_NOTINBRANCH:
13988 if (branch_seen)
13989 {
13990 error_at (OMP_CLAUSE_LOCATION (c),
13991 "%<inbranch%> clause is incompatible with "
13992 "%<notinbranch%>");
13993 remove = true;
13994 break;
13995 }
13996 branch_seen = true;
13997 pc = &OMP_CLAUSE_CHAIN (c);
13998 continue;
13999
14000 default:
14001 gcc_unreachable ();
14002 }
14003
14004 if (!remove)
14005 {
14006 t = OMP_CLAUSE_DECL (c);
14007
14008 if (need_complete)
14009 {
14010 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14011 if (t == error_mark_node)
14012 remove = true;
14013 }
14014
14015 if (need_implicitly_determined)
14016 {
14017 const char *share_name = NULL;
14018
14019 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14020 share_name = "threadprivate";
14021 else switch (c_omp_predetermined_sharing (t))
14022 {
14023 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
14024 break;
14025 case OMP_CLAUSE_DEFAULT_SHARED:
14026 /* const vars may be specified in firstprivate clause. */
14027 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
14028 && TREE_READONLY (t))
14029 break;
14030 share_name = "shared";
14031 break;
14032 case OMP_CLAUSE_DEFAULT_PRIVATE:
14033 share_name = "private";
14034 break;
14035 default:
14036 gcc_unreachable ();
14037 }
14038 if (share_name)
14039 {
14040 error_at (OMP_CLAUSE_LOCATION (c),
14041 "%qE is predetermined %qs for %qs",
14042 t, share_name,
14043 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14044 remove = true;
14045 }
14046 }
14047 }
14048
14049 if (remove)
14050 *pc = OMP_CLAUSE_CHAIN (c);
14051 else
14052 pc = &OMP_CLAUSE_CHAIN (c);
14053 }
14054
14055 if (simdlen
14056 && safelen
14057 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
14058 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
14059 {
14060 error_at (OMP_CLAUSE_LOCATION (simdlen),
14061 "%<simdlen%> clause value is bigger than "
14062 "%<safelen%> clause value");
14063 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
14064 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
14065 }
14066
14067 if (ordered_seen
14068 && schedule_clause
14069 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14070 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
14071 {
14072 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
14073 "%<nonmonotonic%> schedule modifier specified together "
14074 "with %<ordered%> clause");
14075 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14076 = (enum omp_clause_schedule_kind)
14077 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14078 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
14079 }
14080
14081 if (linear_variable_step_check)
14082 for (pc = &clauses, c = clauses; c ; c = *pc)
14083 {
14084 bool remove = false;
14085 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
14086 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
14087 && !bitmap_bit_p (&map_head,
14088 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
14089 {
14090 error_at (OMP_CLAUSE_LOCATION (c),
14091 "%<linear%> clause step is a parameter %qD not "
14092 "specified in %<uniform%> clause",
14093 OMP_CLAUSE_LINEAR_STEP (c));
14094 remove = true;
14095 }
14096
14097 if (remove)
14098 *pc = OMP_CLAUSE_CHAIN (c);
14099 else
14100 pc = &OMP_CLAUSE_CHAIN (c);
14101 }
14102
14103 bitmap_obstack_release (NULL);
14104 return clauses;
14105 }
14106
14107 /* Return code to initialize DST with a copy constructor from SRC.
14108 C doesn't have copy constructors nor assignment operators, only for
14109 _Atomic vars we need to perform __atomic_load from src into a temporary
14110 followed by __atomic_store of the temporary to dst. */
14111
14112 tree
c_omp_clause_copy_ctor(tree clause,tree dst,tree src)14113 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
14114 {
14115 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
14116 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
14117
14118 location_t loc = OMP_CLAUSE_LOCATION (clause);
14119 tree type = TREE_TYPE (dst);
14120 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
14121 tree tmp = create_tmp_var (nonatomic_type);
14122 tree tmp_addr = build_fold_addr_expr (tmp);
14123 TREE_ADDRESSABLE (tmp) = 1;
14124 TREE_NO_WARNING (tmp) = 1;
14125 tree src_addr = build_fold_addr_expr (src);
14126 tree dst_addr = build_fold_addr_expr (dst);
14127 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
14128 vec<tree, va_gc> *params;
14129 /* Expansion of a generic atomic load may require an addition
14130 element, so allocate enough to prevent a resize. */
14131 vec_alloc (params, 4);
14132
14133 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
14134 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
14135 params->quick_push (src_addr);
14136 params->quick_push (tmp_addr);
14137 params->quick_push (seq_cst);
14138 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14139
14140 vec_alloc (params, 4);
14141
14142 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
14143 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
14144 params->quick_push (dst_addr);
14145 params->quick_push (tmp_addr);
14146 params->quick_push (seq_cst);
14147 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14148 return build2 (COMPOUND_EXPR, void_type_node, load, store);
14149 }
14150
14151 /* Create a transaction node. */
14152
14153 tree
c_finish_transaction(location_t loc,tree block,int flags)14154 c_finish_transaction (location_t loc, tree block, int flags)
14155 {
14156 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
14157 if (flags & TM_STMT_ATTR_OUTER)
14158 TRANSACTION_EXPR_OUTER (stmt) = 1;
14159 if (flags & TM_STMT_ATTR_RELAXED)
14160 TRANSACTION_EXPR_RELAXED (stmt) = 1;
14161 return add_stmt (stmt);
14162 }
14163
14164 /* Make a variant type in the proper way for C/C++, propagating qualifiers
14165 down to the element type of an array. If ORIG_QUAL_TYPE is not
14166 NULL, then it should be used as the qualified type
14167 ORIG_QUAL_INDIRECT levels down in array type derivation (to
14168 preserve information about the typedef name from which an array
14169 type was derived). */
14170
14171 tree
c_build_qualified_type(tree type,int type_quals,tree orig_qual_type,size_t orig_qual_indirect)14172 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
14173 size_t orig_qual_indirect)
14174 {
14175 if (type == error_mark_node)
14176 return type;
14177
14178 if (TREE_CODE (type) == ARRAY_TYPE)
14179 {
14180 tree t;
14181 tree element_type = c_build_qualified_type (TREE_TYPE (type),
14182 type_quals, orig_qual_type,
14183 orig_qual_indirect - 1);
14184
14185 /* See if we already have an identically qualified type. */
14186 if (orig_qual_type && orig_qual_indirect == 0)
14187 t = orig_qual_type;
14188 else
14189 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
14190 {
14191 if (TYPE_QUALS (strip_array_types (t)) == type_quals
14192 && TYPE_NAME (t) == TYPE_NAME (type)
14193 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
14194 && attribute_list_equal (TYPE_ATTRIBUTES (t),
14195 TYPE_ATTRIBUTES (type)))
14196 break;
14197 }
14198 if (!t)
14199 {
14200 tree domain = TYPE_DOMAIN (type);
14201
14202 t = build_variant_type_copy (type);
14203 TREE_TYPE (t) = element_type;
14204
14205 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
14206 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
14207 SET_TYPE_STRUCTURAL_EQUALITY (t);
14208 else if (TYPE_CANONICAL (element_type) != element_type
14209 || (domain && TYPE_CANONICAL (domain) != domain))
14210 {
14211 tree unqualified_canon
14212 = build_array_type (TYPE_CANONICAL (element_type),
14213 domain? TYPE_CANONICAL (domain)
14214 : NULL_TREE);
14215 if (TYPE_REVERSE_STORAGE_ORDER (type))
14216 {
14217 unqualified_canon
14218 = build_distinct_type_copy (unqualified_canon);
14219 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
14220 }
14221 TYPE_CANONICAL (t)
14222 = c_build_qualified_type (unqualified_canon, type_quals);
14223 }
14224 else
14225 TYPE_CANONICAL (t) = t;
14226 }
14227 return t;
14228 }
14229
14230 /* A restrict-qualified pointer type must be a pointer to object or
14231 incomplete type. Note that the use of POINTER_TYPE_P also allows
14232 REFERENCE_TYPEs, which is appropriate for C++. */
14233 if ((type_quals & TYPE_QUAL_RESTRICT)
14234 && (!POINTER_TYPE_P (type)
14235 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
14236 {
14237 error ("invalid use of %<restrict%>");
14238 type_quals &= ~TYPE_QUAL_RESTRICT;
14239 }
14240
14241 tree var_type = (orig_qual_type && orig_qual_indirect == 0
14242 ? orig_qual_type
14243 : build_qualified_type (type, type_quals));
14244 /* A variant type does not inherit the list of incomplete vars from the
14245 type main variant. */
14246 if (RECORD_OR_UNION_TYPE_P (var_type)
14247 && TYPE_MAIN_VARIANT (var_type) != var_type)
14248 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
14249 return var_type;
14250 }
14251
14252 /* Build a VA_ARG_EXPR for the C parser. */
14253
14254 tree
c_build_va_arg(location_t loc1,tree expr,location_t loc2,tree type)14255 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
14256 {
14257 if (error_operand_p (type))
14258 return error_mark_node;
14259 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
14260 order because it takes the address of the expression. */
14261 else if (handled_component_p (expr)
14262 && reverse_storage_order_for_component_p (expr))
14263 {
14264 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
14265 return error_mark_node;
14266 }
14267 else if (!COMPLETE_TYPE_P (type))
14268 {
14269 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
14270 "type %qT", type);
14271 return error_mark_node;
14272 }
14273 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
14274 warning_at (loc2, OPT_Wc___compat,
14275 "C++ requires promoted type, not enum type, in %<va_arg%>");
14276 return build_va_arg (loc2, expr, type);
14277 }
14278
14279 /* Return truthvalue of whether T1 is the same tree structure as T2.
14280 Return 1 if they are the same. Return false if they are different. */
14281
14282 bool
c_tree_equal(tree t1,tree t2)14283 c_tree_equal (tree t1, tree t2)
14284 {
14285 enum tree_code code1, code2;
14286
14287 if (t1 == t2)
14288 return true;
14289 if (!t1 || !t2)
14290 return false;
14291
14292 for (code1 = TREE_CODE (t1);
14293 CONVERT_EXPR_CODE_P (code1)
14294 || code1 == NON_LVALUE_EXPR;
14295 code1 = TREE_CODE (t1))
14296 t1 = TREE_OPERAND (t1, 0);
14297 for (code2 = TREE_CODE (t2);
14298 CONVERT_EXPR_CODE_P (code2)
14299 || code2 == NON_LVALUE_EXPR;
14300 code2 = TREE_CODE (t2))
14301 t2 = TREE_OPERAND (t2, 0);
14302
14303 /* They might have become equal now. */
14304 if (t1 == t2)
14305 return true;
14306
14307 if (code1 != code2)
14308 return false;
14309
14310 switch (code1)
14311 {
14312 case INTEGER_CST:
14313 return wi::to_wide (t1) == wi::to_wide (t2);
14314
14315 case REAL_CST:
14316 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
14317
14318 case STRING_CST:
14319 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
14320 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
14321 TREE_STRING_LENGTH (t1));
14322
14323 case FIXED_CST:
14324 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
14325 TREE_FIXED_CST (t2));
14326
14327 case COMPLEX_CST:
14328 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
14329 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
14330
14331 case VECTOR_CST:
14332 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
14333
14334 case CONSTRUCTOR:
14335 /* We need to do this when determining whether or not two
14336 non-type pointer to member function template arguments
14337 are the same. */
14338 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
14339 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
14340 return false;
14341 {
14342 tree field, value;
14343 unsigned int i;
14344 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
14345 {
14346 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
14347 if (!c_tree_equal (field, elt2->index)
14348 || !c_tree_equal (value, elt2->value))
14349 return false;
14350 }
14351 }
14352 return true;
14353
14354 case TREE_LIST:
14355 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
14356 return false;
14357 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14358 return false;
14359 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14360
14361 case SAVE_EXPR:
14362 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14363
14364 case CALL_EXPR:
14365 {
14366 tree arg1, arg2;
14367 call_expr_arg_iterator iter1, iter2;
14368 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14369 return false;
14370 for (arg1 = first_call_expr_arg (t1, &iter1),
14371 arg2 = first_call_expr_arg (t2, &iter2);
14372 arg1 && arg2;
14373 arg1 = next_call_expr_arg (&iter1),
14374 arg2 = next_call_expr_arg (&iter2))
14375 if (!c_tree_equal (arg1, arg2))
14376 return false;
14377 if (arg1 || arg2)
14378 return false;
14379 return true;
14380 }
14381
14382 case TARGET_EXPR:
14383 {
14384 tree o1 = TREE_OPERAND (t1, 0);
14385 tree o2 = TREE_OPERAND (t2, 0);
14386
14387 /* Special case: if either target is an unallocated VAR_DECL,
14388 it means that it's going to be unified with whatever the
14389 TARGET_EXPR is really supposed to initialize, so treat it
14390 as being equivalent to anything. */
14391 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
14392 && !DECL_RTL_SET_P (o1))
14393 /*Nop*/;
14394 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
14395 && !DECL_RTL_SET_P (o2))
14396 /*Nop*/;
14397 else if (!c_tree_equal (o1, o2))
14398 return false;
14399
14400 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14401 }
14402
14403 case COMPONENT_REF:
14404 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14405 return false;
14406 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14407
14408 case PARM_DECL:
14409 case VAR_DECL:
14410 case CONST_DECL:
14411 case FIELD_DECL:
14412 case FUNCTION_DECL:
14413 case IDENTIFIER_NODE:
14414 case SSA_NAME:
14415 return false;
14416
14417 case TREE_VEC:
14418 {
14419 unsigned ix;
14420 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14421 return false;
14422 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14423 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14424 TREE_VEC_ELT (t2, ix)))
14425 return false;
14426 return true;
14427 }
14428
14429 default:
14430 break;
14431 }
14432
14433 switch (TREE_CODE_CLASS (code1))
14434 {
14435 case tcc_unary:
14436 case tcc_binary:
14437 case tcc_comparison:
14438 case tcc_expression:
14439 case tcc_vl_exp:
14440 case tcc_reference:
14441 case tcc_statement:
14442 {
14443 int i, n = TREE_OPERAND_LENGTH (t1);
14444
14445 switch (code1)
14446 {
14447 case PREINCREMENT_EXPR:
14448 case PREDECREMENT_EXPR:
14449 case POSTINCREMENT_EXPR:
14450 case POSTDECREMENT_EXPR:
14451 n = 1;
14452 break;
14453 case ARRAY_REF:
14454 n = 2;
14455 break;
14456 default:
14457 break;
14458 }
14459
14460 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14461 && n != TREE_OPERAND_LENGTH (t2))
14462 return false;
14463
14464 for (i = 0; i < n; ++i)
14465 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14466 return false;
14467
14468 return true;
14469 }
14470
14471 case tcc_type:
14472 return comptypes (t1, t2);
14473 default:
14474 gcc_unreachable ();
14475 }
14476 /* We can get here with --disable-checking. */
14477 return false;
14478 }
14479
14480 /* Returns true when the function declaration FNDECL is implicit,
14481 introduced as a result of a call to an otherwise undeclared
14482 function, and false otherwise. */
14483
14484 bool
c_decl_implicit(const_tree fndecl)14485 c_decl_implicit (const_tree fndecl)
14486 {
14487 return C_DECL_IMPLICIT (fndecl);
14488 }
14489